Ch5.10: const and Pointers
Overview
Pointers and const interact in three distinct ways. Understanding
these combinations is essential for writing correct and safe C++ code.
In this chapter, you will learn:
- the rule:
constapplies to the left. Full stop. - how to read
T const*,T* const, andT const* const - the difference between “pointer to const” and “const pointer”
- why
char const*is the correct type for C‑style strings - how const interacts with pointer arithmetic
1. The rule: const applies to the left. Full stop.
This rule was introduced in Ch2.8 and is the foundation for reading all const pointer types:
If there is a type to the left of const, it applies to that type.
If not, it applies to the right.
Examples:
int const x = 5; // const applies to int
const int y = 6; // same meaning, but less clear
For pointers, this rule becomes extremely important.
2. Pointer to const: T const*
This means “pointer to a const T”. The pointer may change, but the object it points to may not.
int value{10};
int const *p = ::std::addressof(value); // pointer to const int
Allowed:
p = ::std::addressof(value); // ✔ pointer may change
Not allowed:
// *p = 20; // ❌ cannot modify through pointer
This is the correct type for pointers to C‑style strings:
char const *s = "Hello"; // correct
Always write T const*, not const T*.
3. Const pointer: T* const
This means “const pointer to T”. The pointer may not change, but the object it points to may.
int value{10};
int * const p = ::std::addressof(value); // const pointer
Allowed:
*p = 20; // ✔ can modify the pointee
Not allowed:
// p = nullptr; // ❌ pointer itself is const
4. Const pointer to const: T const* const
This means:
- the pointer cannot change
- the object pointed to cannot change
int value{10};
int const * const p = ::std::addressof(value);
Not allowed:
// p = nullptr; // ❌ pointer is const
// *p = 20; // ❌ pointee is const
5. Why char const* is the correct type for C‑style strings
A string literal is a constant C‑style array. It cannot be modified.
char const *s = "Hello"; // correct
Writing char const* makes the constness explicit and follows the rule:
const applies to the left. Full stop.
Writing const char* is allowed but less consistent with the rule.
6. Const and pointer arithmetic
Const does not affect pointer arithmetic. It only affects what may be modified.
int a[3]{1,2,3};
int const *p = a; // pointer to const int
p = p + 1; // ✔ pointer may move
But you still cannot modify the pointee:
// *p = 10; // ❌ not allowed
For a const pointer:
int * const q = a; // const pointer
// q = q + 1; // ❌ cannot move pointer
*q = 10; // ✔ can modify pointee
7. Summary table
| Syntax | Meaning | Pointer can change? | Pointee can change? |
|---|---|---|---|
T const* |
pointer to const T | ✔ yes | ❌ no |
T* const |
const pointer to T | ❌ no | ✔ yes |
T const* const |
const pointer to const T | ❌ no | ❌ no |
Key takeaways
constapplies to the left. Full stop.- Always write
T const*, notconst T*. T const*means “pointer to const T”.T* constmeans “const pointer to T”.T const* constmeans “const pointer to const T”.char const*is the correct type for C‑style strings.- Const does not affect pointer arithmetic.
- Const only affects what may be modified.