I don't quite get what you are trying to ask. In your example the "hello world"
is stored in the read-write area, but the pointer to it isn't, since it wasn't declared as const
.
const char* string
means, this is a mutable pointer to an constant string.
To cause the pointer being stored in the read-only area, too, you would have to declare it as const
:
const char* const string = "hello world";
const
I general all modifiers in the C and C++ languages bind to the value to it's left. Only if there is nothing to the left, they apply to the next to their right. Hence these two are the same:
char const* str; // Bind const modifier to 'char'.
const char* str; // Bind const modifier to 'char', since there is nothing to the left.
When looking at this, you need to be aware of that a pointer declaration consists of 2 "parts". The type the pointer points to, and the pointer itself.
Modifiers like const
or volatile
can be applied to both of these parts:
char* const str; // A constant pointer, that points to a mutable char (array).
Here a few examples, how const
affects variables:
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.