大头
分类
评论
留言
链接
标签云
热门文章
随机文章
页面
豆瓣上谁关注这里
在读
计数器
3821
访客统计
外部变量申明的一个错误
C++中的外部变量不是类型安全的变量。
File: sub.cpp
-
// the string to print
-
char str[] = "Hello World!\n";
File: main.cpp
-
#include <iostream>
-
-
extern char *str; // the string to print
-
-
int main()
-
{
-
std::cout<< str << std::endl;
-
return (0);
-
}
在这里,str被认为是一个指针,程序运行到那儿,读其地址前4位,而前4位是“Hell”,不是一个地址,所以出错。
