C++: const Pointer Declarations

Coding · Easy · Free problem

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

  1. Apply the right-to-left reading rule: starting from the variable name, read each token outward and ask what const is binding to.
  2. const modifies whatever is immediately to its left -- so in const char*, ask: is const to the left of char or to the left of *?
  3. The critical distinction: if const comes before the *, the data is immutable (pointer-to-const). If const comes 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 -- p1 is a * (pointer) to char that is const. The const is to the left of char, so it modifies the character. The characters are immutable; the pointer can be reassigned.
  • char const* p2: Identical to const char*. The const is still to the left of * and to the right of char -- it modifies the char. Same semantics as p1.
  • char* const p3: Here const is to the right of * -- 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 = buf2; // 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. Getting this wrong compiles silently in some contexts (especially with implicit conversions) but leads to subtle bugs. The habit of reading declarations right-to-left will serve you in any C or C++ codebase.

Open the full interactive solver →