本文共 748 字,大约阅读时间需要 2 分钟。
由于需要, 所以小写一下。
main.cpp中的代码为:
#includetest.cpp中的代码为:using namespace std;extern char str[];int main(){ cout << str << endl; return 0;}
char str[100] = "abcdefg";程序的结果为:abcdefg
下一道菜:
main.cpp
#includetest.cppusing namespace std;void print();extern char str[];int main(){ cout << str << endl; // cout << sizeof(str) << endl; //编译错误 cout << strlen(str) << endl; // 7 strcpy(str, "xyz"); cout << str << endl; print(); // 输出hello world, xyz, 可见, 上面的strcpy改变了test.cpp中的值 return 0;}
#include输出结果:char str[100] = "abcdefg";void print(){ printf("hello world, %s\n", str);}
abcdefg
7 xyz hello world, xyz
最后看一下void print();和(void)print();的区别:
void print();是生命, 而(void)print();是调用, 在代码中非常常见, 为什么要加(void)呢, 多数为了通过静态检查工具的检查。
转载地址:http://ylrvi.baihongyu.com/