一直的以为,指向常量的字符串的指针可以改变这个字符串的数据,但现在却不认为如此,以前的思维是错误的。
以下程序将会出现错误(运行时错误):
char * pConst = "123456789";
int main(int argc, char* argv[]) {
printf("change before:%s/n",pConst);
pConst[0] = 'a'; // this will make error; it will get the error : "unhandled exception in xxxx.exe: x000xxxx :Access Violation"
printf("change after:%s/n",pConst);
getchar();
return 0;
}
运行的时候会出现运行时错误,程序的错误提示非法存储内存地址,因为常量不能修改。希望以前像我这样认为的程序员值得注意,请运行上面的代码吧``