首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于extern的疑义

2013-03-06 
关于extern的疑问Test.c 文件#include stdio.hchar* const hellochar hello1[3]// 这里面的hello1是ch

关于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访问的是相同的地址

热点排行