读书人

关于extern的疑义

发布时间: 2013-03-06 16:20:31 作者: rapoo

关于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


麻烦大神们,帮忙解释一下为什么会出现这种情况?

[解决办法]
引用:
hello1,hello2在Test.c中是字符数组,在Test1.c里面就变成了字符,这样对吗?

C语言仅在编译时进行类型检查, 链接的时候和运行是不检查类型的。
Test.c 里的char hello1[3] 和 Test1.c里的char hello1访问的是相同的地址

读书人网 >C语言

热点推荐