【发布时间】:2019-07-26 05:15:41
【问题描述】:
举个例子:
int main()
{
const char* what = "Is This";
what = "Interesting";
cout << *what;
what[3] = 'a'; // Sytax Error: expression must be a modifiable lvalue
cout << *what;
return 0;
}
所以我将what 声明为const char*,然后我能够重新为其分配另一个值(内存中的实际数据 - 而不是内存地址本身)。
但是,它告诉我,我无法更改第 4 位的角色!
这是为什么呢?
【问题讨论】:
-
在您的示例中,
what是可修改的。它指向的内容是不可修改的。 -
查看这个问题了解如何阅读指针声明:stackoverflow.com/questions/1143262/…
-
如果你想要一个指向你正在寻找的 const 事物的 const 指针
const char* const what
标签: c++ arrays pointers constants