总结:将数组元素类型最好定义为string类型,而不是char*类型(VS2017无法通过)
#include <iostream>
using namespace std;
int main()
{
char* const a[7] = { "Hello", "world" };
//会出现[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
//Dev编译器下:极不赞成 、强烈反对上面这种定义方式
//在VS2017下,则编译不会 通过
char b[7][10] = { "A sunny", "day!" };
cout<<a[0]<<endl;
cout<<b[1]<<endl;
char* c=b[1];
cout<<c<<endl;
return 0;
}```
比较好的方法:用string类型
#include<string>
#include<iostream>
int main()
{
using namespace std;
string str[3] = {"ert","asd", "cvb"};
for(int i = 0; i < 3; i ++)
{
cout<<str[i]<<endl;
}
}
运行这段程序就可以了,是在vs2008上测试过。注意两个地方
1.#include<string>
2.using namespace std;
千万不要include string.h啦,string.h是C里的字符串库,而string是C++库,两者没有关系。这要弄明白连个库有什么不同,在网上搜搜就有。
C++里的String是包含在std命名空间里的,所以要加上第二句。
//部分来源:https://zhidao.baidu.com/question/424851168.html