在C++中,为什么编译器不允许修改以下字符指针如下访问字符指针
#include <iostream>
int main()
{
char* cp = "overflow";
cp[1]='p';
return 0;
}
输出:在运行时崩溃。
但字符数组允许,
#include <iostream>
int main()
{
char cps[] = "overflow";
cp[1]='p'; // this compiles fine and output is operflow
return 0;
}
只是想知道什么是在运行时发生的事情,为何导致崩溃。谢谢。
'char cps =“overflow”;'是错的,你的意思是'char cps [] =“overflow”;'? – unwind 2013-04-10 11:35:28
@unwind对不起,它失踪了。更正.. – 2013-04-10 11:39:03