In C++, what is the difference between const int*, int* const and const int* const?
answer
- read the declaration right to left
- const binds to what is on its left
- pointee versus the pointer itself
- int const* equals const int*
basics
~20 sconst int* is a pointer to a const int: the pointer can move but cannot write through. int* const is a const pointer to int: it cannot be reseated but can write. const int* const forbids both.
solid answer
~50 sRead the declarator right to left, with `const` applying to whatever is immediately to its left (or to the right when it comes first). `const int* p` (same as `int const* p`) is a *pointer to const int*: `p = other` is fine, `*p = 1` is rejected. `int* const p` is a *const pointer to int*: `*p = 1` is fine, `p = other` is rejected, so it must be initialized. `const int* const p` forbids both. The const on the pointee is a promise about *this access path*, not about the object: another non-const pointer may still change it. For function parameters the top-level `const` in `int* const p` is not part of the function's type, while the low-level `const` in `const int* p` is, and it is what lets a caller pass read-only data.
code
cpp · 17 lines#include <cstdint>
int main() {
std::uint8_t pixels[4] = {10, 20, 30, 40};
std::uint8_t other[4] = {};
const std::uint8_t* src = pixels; // pointer to const: *src is read-only
src = other; // OK: the pointer itself may move
std::uint8_t* const dst = pixels; // const pointer: dst cannot be reseated
*dst = 99; // OK: the pixel is writable
const std::uint8_t* const lut = pixels; // neither may change
// *src = 1; // error: read-only variable is not assignable
// dst = other; // error: cannot assign to variable 'dst' with const-qualified type
// lut = other; // error: cannot assign to variable 'lut' with const-qualified type
return *dst + *lut + *src; // 99 + 99 + 0
}go deeper
Recall the right-to-left reading and say which of the pointer and the pointee each const protects in const int*, int* const and const int* const.
Explain top-level versus low-level const, why top-level const on a parameter is not part of the function type, and why a const pointer must be initialized.
Show why int** does not convert to const int** but does convert to const int* const*, and why pointee-const is only a promise about one access path.
Set a house style for const placement and for const-correct read-only buffer parameters, so library interfaces can accept const data without casts.
## Reading a pointer declaration A pointer declaration has two places a `const` can attach: to the **pointee** (the object you reach by dereferencing) and to the **pointer object** itself. The reliable way to read it aloud is right to left, applying each `const` to the thing on its left: | Declaration | Read right to left | `p = other;` | `*p = 1;` | |---|---|---|---| | `int* p` | p is a pointer to int | allowed | allowed | | `const int* p` / `int const* p` | p is a pointer to const int | allowed | rejected | | `int* const p` | p is a const pointer to int | rejected | allowed | | `const int* const p` | p is a const pointer to const int | rejected | rejected | When `const` is the first token, it applies to the type on its right, which is why `const int*` and `int const*` (the "east const" spelling) name the same type. A `const` placed after the `*` is **top-level**: it is about the pointer variable, so the variable must be initialized and cannot be reassigned. ## What the compiler says In an image filter, a source buffer is typically `const std::uint8_t* src` (read-only pixels, movable cursor), and a fixed destination is `std::uint8_t* const dst`. Clang's diagnostics name the exact rule you broke: - writing `*src = 1` gives `read-only variable is not assignable`; - reseating `dst = other` gives `cannot assign to variable 'dst' with const-qualified type 'std::uint8_t *const'`. The type printed in the second message, `std::uint8_t *const`, is the compiler reading the declaration exactly as the table does. ## Low-level const is a view, not a guarantee `const int* p` means "the program may not modify the object **through p**". It says nothing about the object itself: ```cpp int level = 3; const int* view = &level; level = 4; // fine: level is not const // *view == 4 now ``` The object can be changed through another name, and the compiler must assume that it can. Only an object **defined** `const` is immutable; casting the const away with `const_cast` and writing to such an object is undefined behaviour. Casts in general are covered with casts and conversions; here the point is just that the pointer's `const` describes an access path. ## Conversions: adding const is safe, but not at every level Converting `int*` to `const int*` is an implicit **qualification conversion**: you only lose rights. One level deeper, the rule surprises people: 1. `int** pp` to `const int* const* q` compiles: every level that gains `const` above the bottom is also `const`. 2. `int** pp` to `const int** q` is **rejected**. If it were allowed, `*q = &some_const_int;` would store a pointer to a const object into the `int*` that `pp` points at, and a later `**pp = 5` would modify a const object with no cast in sight. This is the classic senior follow-up, and the answer is about soundness, not size or representation - all these pointer types have the same object representation on mainstream targets. ## const in function signatures - `void blit(const std::uint8_t* src, std::uint8_t* dst, std::size_t n);` - the low-level `const` is part of the function type and is the contract: the caller can pass a read-only buffer. - `void blit(std::uint8_t* const dst);` - the top-level `const` is **dropped** from the function's type, so `void blit(std::uint8_t*)` and `void blit(std::uint8_t* const)` declare the same function. It only constrains the body. - A `const` member function makes `this` a pointer to const, which is why such functions can be called on const objects. The same reading applies to `const` on member pointers and to `volatile`: `volatile std::uint32_t* const reg` is a fixed pointer to a volatile register, the shape memory-mapped I/O code uses, and the register semantics themselves belong to the material on volatile. ## The interview checklist - Say which object is const - the pointee or the pointer - before you say anything else. - Show that `const int*` and `int const*` are the same type. - Point out that a `T* const` must be initialized. - Mention that pointee-const is a promise about one access path, and that `int**` to `const int**` is rejected for soundness.
- Why does C++ reject converting int** to const int** when it accepts int* to const int*?Because the deeper conversion would open a hole in const safety. Through a `const int** q` that aliases `int** pp`, you could store the address of a `const int` into `*q`, which is the same `int*` that `*pp` names, and then write `**pp = 5` - modifying a const object without a cast. Converting to `const int* const*` is allowed because the middle level can no longer be assigned.
- Does const int* p guarantee that *p never changes while you hold p?No. It only forbids modification through `p`. The object may be non-const and changed through another pointer, a reference or its own name, and the compiler has to assume it can be. Only an object defined `const` is guaranteed immutable, and writing to one after `const_cast` is undefined behaviour.
- const int* means the pointer cannot be reassigned
- int const* and const int* are different types
- A const int* guarantees nobody else can change the int
- An int* const can be declared without an initializer
- int** converts implicitly to const int** because adding const is always safe