关于extern的疑问
Test.c 文件
#include <stdio.h>
char* const hello;
char hello1[3]; // 这里面的hello1是char数组
void show(void) {
printf("hello:%s\n", hello);
printf("hello1:%s\n", hello1); // 输出居然是:hello1:c 这是我不理解的地方
}
Test1.c 文件
#include <stdio.h>
extern char* hello;
extern char hello1; // 这里hello1是char
extern void show();
int main(void) {
hello = "abcdef";
hello1 = 'c'; // 给字符赋值
show(); // 打印
return 0;
}
输出结果:
hello:abcdef
hello1:c
麻烦大神们,帮忙解释一下为什么会出现这种情况?
[解决办法]
C语言仅在编译时进行类型检查, 链接的时候和运行是不检查类型的。
Test.c 里的char hello1[3] 和 Test1.c里的char hello1访问的是相同的地址