W
wy
This code can't be compiled without errors.
/* test.c */
#include <stdio.h>
typedef char data_str;
data_str _data_[3], *data = _data_;
void data_pointer_print(){
printf("_data_ address 1st: %p\n", _data_);
printf(" data address 2nd: %p\n", data );
}
extern data_str data[3];
int main(){
data_pointer_print();
printf(" data address 3rd: %p\n", data );
}
I understand the error "conflicting types for ‘data’".
But after splitting the code into several files,
it is compiled successfully, and have an undesired result.
/* data.h */
#include <stdio.h>
typedef char data_str;
extern void datastr_pointer_print();
/* data.c */
#include "data.h"
data_str _data_[3], *data = _data_;
void data_pointer_print(){
printf("_data_ address in data.c: %p\n", _data_);
printf(" data address in data.c: %p\n", data );
}
/* test.c */
#include "data.h"
extern data_str data[3];
int main(){
data_pointer_print();
printf(" data address in test.c: %p\n", data );
}
And this is the result:
$ gcc test.c data.c
$ ./a.out
_data_ address in data.c: 0x6009b8
data address in data.c: 0x6009b8
data address in test.c: 0x6009a0
Here we see, it's compiled with no errors and warnings,
and the data addresses in data.c and test.c are different.
Why are they different and
why don't c complier prohibit this kind of use?
/* test.c */
#include <stdio.h>
typedef char data_str;
data_str _data_[3], *data = _data_;
void data_pointer_print(){
printf("_data_ address 1st: %p\n", _data_);
printf(" data address 2nd: %p\n", data );
}
extern data_str data[3];
int main(){
data_pointer_print();
printf(" data address 3rd: %p\n", data );
}
I understand the error "conflicting types for ‘data’".
But after splitting the code into several files,
it is compiled successfully, and have an undesired result.
/* data.h */
#include <stdio.h>
typedef char data_str;
extern void datastr_pointer_print();
/* data.c */
#include "data.h"
data_str _data_[3], *data = _data_;
void data_pointer_print(){
printf("_data_ address in data.c: %p\n", _data_);
printf(" data address in data.c: %p\n", data );
}
/* test.c */
#include "data.h"
extern data_str data[3];
int main(){
data_pointer_print();
printf(" data address in test.c: %p\n", data );
}
And this is the result:
$ gcc test.c data.c
$ ./a.out
_data_ address in data.c: 0x6009b8
data address in data.c: 0x6009b8
data address in test.c: 0x6009a0
Here we see, it's compiled with no errors and warnings,
and the data addresses in data.c and test.c are different.
Why are they different and
why don't c complier prohibit this kind of use?