请问一下这里为什么报错了?
#include <iostream>
using namespace std;
#include <string>
int main()
{
//C style
char str1[] = "hello world";
//C++ style
string str2 = "hello world";
cout << "str1=" << str1 << endl;
cout << "str1空间是" << sizeof(str1) << endl;
cout << "str1 ACSII码是" << (int)str1 << endl;
cout << "str2空间是" << sizeof(str2) << endl;
cout << "str2 ACSII码是" << (int)str2 << endl; //这个地方报错,为什么?
cout << "str2=" << str2 << endl;
system("pause");
return 0;
}
这里牵扯到了数组名的知识,strl时字符型数组的数组名,他代表这一数组的首地址,是个地址,因此可以被(int)转换为整型数据,然而str2只是一串字符串,可以用sizeof关键字计算所占空间内存大小,但是他不是地址,而是一个个字符,不能一下子全变成整型数据。
只能一个一个字符的转