由于需要, 所以小写一下。


     main.cpp中的代码为:

#include <iostream>
using namespace std;

extern char str[];
int main()
{
	cout << str << endl;
	return 0;
}

     test.cpp中的代码为:

char str[100] = "abcdefg";

     程序的结果为:abcdefg



     下一道菜:

     main.cpp

#include <iostream>
using 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;
}


    test.cpp

#include <stdio.h>
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)呢, 多数为了通过静态检查工具的检查。


     


本文转载:CSDN博客