【发布时间】:2013-10-22 18:27:43
【问题描述】:
我知道const char * 是一个指向常量字符的指针,而char *const 是一个指向字符的常量指针。
我正在以下代码中对此进行测试:
const char *s = "hello"; // Not permitted to modify the string "hello"
char *const t = "world"; // Not permitted to modify the pointer t
s = "hello2"; // Valid
// t = "world2"; // Invalid, gives compilation error
// *(s + 1) = 'a'; // Invalid, gives compilation error
*(t + 1) = 'a'; // Why does this not work?
最后一行没有给出任何错误,但会导致程序意外终止。为什么不允许修改t指向的字符串?
【问题讨论】:
标签: c++ c string pointers runtime-error