最近遇到这么一个问题:
int main(){
//char* p = "aaa"; /* will crash later */
char p[] = "aaa"; /* good */ *p = 'b'; return 0; }查《c programming language》发现:
There is an important difference between these definitions:
char amessage[] = "now is the time"; /* an array */ char *pmessage = "now is the time"; /* a pointer */amessageis an array, just big enough to hold the sequence of characters and '\0'that initializes it. Individual characters within the array may be changed but amessage will always refer to the same storage. On the other hand, pmessageis a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents.
原来char* p = "aaa";这种定义方法,p指向的内容是不能修改了,如果强制修改,结果是未知的!