-
#include <iostream>
-
-
using namespace std;
-
-
-
int main(int argc, char* argv[])
-
{
-
char bufferArray[] = "abc";
-
cout << "bufferArray = " << bufferArray << endl;
-
//
-
// Change OK
-
//
-
bufferArray[0] = 'A';
-
cout << "--------------------------------------------------" << endl;
-
cout << "After Change" << endl;
-
cout << "bufferArray = " << bufferArray << endl;
-
-
-
char* bufferPointer = "abc";
-
cout << "bufferPointer = " << bufferPointer << endl;
-
-
bufferPointer[0] = 'A'; // Run-Time Error
-
cout << "--------------------------------------------------" << endl;
-
cout << "After Change" << endl;
-
cout << "bufferPointer = " << bufferPointer << endl;
-
return 0;
-
}
运行结果如下:
bufferArray = abc
--------------------------------------------------
After Change
bufferArray = Abc
bufferPointer = abc
Bus error: 10
因此说明了一件事,
字符串常量 如果赋值给一个字符数组,那么这个字符串中的信息是
可以被修改的
字符串常量 如果赋值给一个字符串的指针,那么这个字符串的信息是不可以被修改的
阅读(1121) | 评论(0) | 转发(0) |