64,637
社区成员
发帖
与我相关
我的任务
分享
char *p = "world";
p[0] = 'x';
#include <iostream>
using namespace std;
void main()
{
char a[] = "hello";
a[0] = 'x';
cout << a << endl;
char *p = "world";
/*限定词c o n s t是很严格的,const没被强调的地方是有关串字面值。也许有人写:
char* cp="world";
编译器将接受它而不报告错误。从技术上讲,这是一个错误,因为串字面值(这里是
“world”)是被编译器作为一个常量串建立的,所引用串的结果是它在内存里的首地址。
所以串字面值实际上是常量串。然而,编译器把它们作为非常量看待,这是因为有许多现
有的C代码是这样做的。当然,改变串字面值的做法还未被定义,虽然可能在很多机器上是这
样做的。
如果想修改字符串,就要把它放到一个数组中:
char p[] = "world";
*/
p[0] = 'x';
cout << p << endl;
}
#include <iostream>
using namespace std;
void main()
{
char a[] = "hello";
a[0] = 'x';
cout << a << endl;
char *p;
p=new char[10];//动态分配空间
strcpy(p,"world");
p[0] = 'x';
cout << p << endl;
delete[] p;
}