Advertisement
Intermediate C++, Const Correctness

What is the difference between const pointer and pointer to const?

Short answer

'Pointer to const' (const int* p) means the pointed-to value can't be changed through p, but p can point elsewhere. 'Const pointer' (int* const p) means p can't be repointed, but the value can change. 'Const pointer to const' (const int* const p) fixes both.

Save C++ mock interview
Advertisement
AModel answer
'Pointer to const' (const int* p) means the pointed-to value can't be changed through p, but p can point elsewhere. 'Const pointer' (int* const p) means p can't be repointed, but the value can change. 'Const pointer to const' (const int* const p) fixes both. Reading right-to-left clarifies which part is const.
const int* a;      // value const, pointer mutable
int* const b = &x; // pointer const, value mutable
const int* const c = &x; // both const
Real-world example
An API returns a const int* so callers can read but not modify the internal buffer, while reusing the pointer.
Likely follow-ups
  • How do you read the declarations? | Which part does each const fix?
Related topics