我不太明白你想问什么。在您的示例中,"hello world" 存储在读写区域中,但 指向它的指针 没有,因为它没有被声明为 const。
const char* string 表示,这是一个可变指针,指向一个常量字符串。
要使指针也存储在只读区域中,您必须将其声明为const:
const char* const string = "hello world";
常量
我一般 C 和 C++ 语言中的所有修饰符都绑定到它的 left 的值。仅当左侧没有任何内容时,它们才适用于其右侧的下一个。因此这两个是相同的:
char const* str; // Bind const modifier to 'char'.
const char* str; // Bind const modifier to 'char', since there is nothing to the left.
查看此内容时,您需要注意指针声明由 2 个“部分”组成。指针指向的类型,以及指针本身。
const 或 volatile 等修饰符可以应用于这两个部分:
char* const str; // A constant pointer, that points to a mutable char (array).
这里有几个例子,const 如何影响变量:
char const* a = "foo";
*a = '\0'; // Error, the array pointed to is not mutable.
a = "bar"; // Okay.
char* const b = "foo";
*b = '\0'; // Okay.
b = "bar"; // Error, the pointer is not mutable.