C++: const Pointer Declarations
In C++, what is the difference between these three declarations?
```cpp const char* p1; char const* p2; char* const p3; ```
Which can you reassign? Which let you modify the underlying characters?
Hints
- Apply the right-to-left reading rule: starting from the variable name, read each token outward and ask what
constis binding to. constmodifies whatever is immediately to its left, or, if nothing is to its left, whatever is immediately to its right. So inconst char*and inchar const*, ask: doesconstattach tocharor to*?- The critical distinction: if
constcomes before the*, the data is immutable (pointer-to-const). Ifconstcomes after the*, the pointer itself is immutable (const-pointer).
Worked Solution
How to Think About It: The trick to reading C++ const declarations is to apply the right-to-left rule: read from the variable name outward, parsing each token as it appears. The word const modifies whatever is immediately to its left, and if nothing is to its left, it modifies what is immediately to its right. The key question in each case is: does const bind to the char (the data is immutable) or to the * (the pointer itself is immutable)?
Algorithm:
For each declaration, read right-to-left from the variable name:
const char* p1: Reading right-to-left,p1is a*(pointer) tocharthat isconst. Nothing is to the left ofconst, so it binds to thecharon its right: it modifies the character. The characters are immutable; the pointer can be reassigned.
char const* p2: Identical toconst char*. Hereconstsits immediately to the right ofchar(and still before the*), so it binds to thechar: it modifies the character. Same semantics asp1.
char* const p3: Hereconstsits immediately to the right of*, so it binds to the pointer: it modifies the pointer itself, not the data. The pointer cannot be reassigned; the characters it points to can be modified.
Summary table:
| Declaration | Pointer reassignable? | Data modifiable? | |---|---|---| | const char* | Yes | No | | char const* | Yes | No | | char* const | No | Yes |
Code examples: ```cpp const char* p1 = "hello"; p1 = "world"; // OK: pointer reassigned // *p1 = 'H'; // ERROR: data is const
char buf[] = "hello"; char* const p3 = buf; *p3 = 'H'; // OK: data is mutable // p3 = buf + 1; // ERROR: pointer is const ```
Answer: const char* and char const* are identical: both are pointers to immutable characters (pointer can be moved, data cannot be changed). char* const is a constant pointer to mutable characters (pointer cannot be moved, data can be changed). const binds to whatever is immediately to its left; if nothing is to its left, it binds to the right.
Intuition
This is a purely syntactic question, but it tests whether you understand how C++ declaration parsing works. The right-to-left rule is not arbitrary: it reflects the C declarator grammar, where types are built up by reading modifiers outward from the identifier. const binding to char versus binding to * has completely different runtime implications: one prevents you from writing through the pointer, the other prevents you from redirecting the pointer entirely.
In practice this matters for API design and correctness guarantees. A function that takes const char* promises it will not modify your string. A char* const member in a class means the pointer is fixed at construction but the buffer it points to can be mutated. The compiler catches direct violations, but misreading which part is const (for example, assuming a char* const parameter protects your buffer) leads to wrong assumptions about who can modify what. The habit of reading declarations right-to-left will serve you in any C or C++ codebase.