C指针行为,第二卷

Mad*_*ter 1 c pointers

看这个:

int main() {

char *verse = "zappa";
printf("%c\n", *verse);
// the program correctly prints the first character

*verse++;
printf("%c\n", *verse);
// the program correctly prints the second character, which in fact lies 
// in the adjacent memory cell

(*verse)++;
printf("%c\n", *verse);
// the program doesn't print anything and crashes. Why?

return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么我的程序会崩溃,因为我尝试增加verse指向的值?我期待像ASCII表中的下一个字符.

Bil*_*nch 10

此行(*verse)++;修改字符串文字.这是未定义的行为.

请注意,较早的代码行*verse++被解析为*(verse++)