The difference between const int and int const** comes down to whether const locks the pointed-to value (a pointer to const) or locks the pointer variable itself (a const pointer). Based on cppreference’s pointer declaration documentation and results measured in this post’s environment (g++ (Debian 14.2.0-19) 14.2.0, x86-64 Linux), this post explains a single rule for reading where const goes on a C++ pointer along with the exact compiler errors produced when each lock is violated.

Key Takeaway

const int* locks the pointed-to value, whereas int* const locks the pointer itself. The dividing line is *: const to the left of * makes the value const, and const to the right of * makes the pointer const, which is why const int* and int const* are the same type. Reading a declaration from the variable name leftward keeps the meaning clear. Writing const T* or const T& on function parameters lets the compiler verify the promise that the function will not modify the argument.

What is the difference between const int* and int* const?

One-line answer: const int* locks the pointed-to value (the pointer can move), whereas int* const locks the pointer itself (the pointed-to value can change).

In C++, const int* p is a pointer to a constant int: you cannot modify the integer through p, but you can re-point p to another address such as p = &b;. Conversely, int* const q is a constant pointer to a non-const int: once initialized, q cannot be re-pointed to another address, but it can modify the target integer with *q = 10;. Putting const on both sides—const int* const r—locks both the pointer and the value.

The following program, ok.cpp, collects only the allowed operations on all three declarations and was built and run with g++ in this post’s environment. Its Korean code comments say “cannot change the pointed-to value, can change the pointer” (line for p) and “pointer fixed, value can change” (line for q).

#include <cstdio>
int main(){ int a=1,b=2;
 const int* p=&a; p=&b;          // 가리키는 값 변경 불가, 포인터는 변경 가능
 int* const q=&a; *q=10;         // 포인터 고정, 값 변경 가능
 const int* const r=&b;
 std::printf("*p=%d a=%d *r=%d\n",*p,a,*r); }

Running the program produces:

*p=2 a=10 *r=2

Here, p was re-pointed from a to b (which holds 2), a was changed to 10 through q, and r points to b (2). For the basics of pointers and memory addresses, see the post on C pointers and memory layout (Korean).

Diagram comparing where const locks the pointed-to value (const int*) versus the pointer variable itself (int* const), using * as the dividing line

How should you read a pointer declaration?

One-line answer: Split the declaration at *: const on the left locks the value, const on the right locks the pointer, and reading from the variable name leftward makes the meaning clear.

According to cppreference’s pointer declaration documentation, if a cv qualifier (const or volatile) appears before *, it is part of the declaration specifier sequence and applies to the pointed-to object; if it appears after *, it is part of the declarator and applies to the pointer being declared.

As a helpful reading tip consistent with that grammar rule (a mnemonic rather than a formal standard rule), you can read the declaration from the variable name leftward, treating * as the dividing line where const to the left locks the value and const to the right locks the pointer:

  1. const int* p: reading leftward from p gives “p is a pointer (*) to an int that is const” (a pointer to const int).
  2. int* const q: reading leftward from q gives “q is a const pointer (* const) to int.”
  3. const int* const r: reading leftward from r gives “r is a const pointer (* const) to const int.”

Diagram showing how to read pointer declarations leftward from the variable name and where const applies relative to *

What does const int* const prevent?

One-line answer: It prevents both modifying the pointed-to value and re-pointing the pointer to a different address at compile time.

Assigning through a pointer to const (*p=3, *r=1) or re-pointing a const pointer variable (q=&b, r=&b) is rejected at compile time. The one-line file err.cpp below attempts each disallowed assignment:

int main(){ int a=1,b=2; const int* p=&a; *p=3; int* const q=&a; q=&b; const int* const r=&a; *r=1; r=&b; }

Here are the four compiler error lines measured in this post’s environment (g++ (Debian 14.2.0-19) 14.2.0, x86-64 Linux):

err.cpp:1:45: error: assignment of read-only location ‘* p’
err.cpp:1:67: error: assignment of read-only variable ‘q’
err.cpp:1:97: error: assignment of read-only location ‘*(const int*)r’
err.cpp:1:102: error: assignment of read-only variable ‘r’

The wording of each diagnostic matches the position of const: assignment of read-only location means the pointed-to value is locked (triggered by *p=3 and *r=1), whereas assignment of read-only variable means the pointer variable itself is locked (triggered by q=&b and r=&b). Because r is declared const int* const, it triggers both errors.

Are const int* and int const* the same type?

One-line answer: Yes, both place const before *, so they are the exact same type: a pointer to a constant object.

Whenever const appears before *, it is part of the declaration specifier sequence and applies to the pointed-to object regardless of whether it is written before or after the type name. Just as const int n2 and int const n3 have the same type for non-pointer variables, const int* and int const* are identical pointer types. The table from cppreference’s pointer declaration documentation summarizes all five forms:

Declaration syntaxMeaningNotes
const T*pointer to constant objectThe pointed-to value is const
T const*pointer to constant objectSame type as const T*
T* constconstant pointer to objectThe pointer itself is const
const T* constconstant pointer to constant objectBoth pointer and value are const
T const* constconstant pointer to constant objectSame type as const T* const

Why use const T* or const T& for function parameters?

One-line answer: It lets the compiler enforce that the function only reads the argument, while allowing callers to pass both const and non-const objects.

As documented on cppreference’s cv qualifier page, pointers and references to cv-qualified types can be implicitly converted to pointers and references to more cv-qualified types. That means an int* converts implicitly to const int*, and an int& converts implicitly to const int&, so callers can pass non-const objects without a cast. Converting in the opposite direction—to a less cv-qualified type—requires const_cast, so assigning a const int* value to an int* variable as-is is a compile-time error.

For example, with the illustrative function signatures below (shown as a signature example with no program output), the compiler rejects any accidental attempt inside the function to modify the array elements or the string through data or s:

int sum(const int* data, int n);
void print(const std::string& s);

What mistakes are common with const pointers?

One-line answer: const on a pointer only restricts access through that pointer rather than freezing the underlying variable, and casting const away to write to a const object is undefined behavior.

  • Assuming the pointed-to object cannot change through another path: Writing const int* p = &a; only prevents modifying a through p. If a itself is not declared const, it can still be modified directly or through another non-const pointer, and *p will then read the new value. In ok.cpp above, a was changed to 10 through q (int* const) even while p was a pointer to const, so *p is not a frozen snapshot.
  • Using const_cast to write to a const object: As documented on cppreference’s cv and const_cast pages, modifying a const object directly is a compile-time error, and modifying it indirectly through a pointer or reference to non-const type obtained via const_cast (for example, const int j = 3; int* pj = const_cast<int*>(&j); followed by *pj = 4;) is undefined behavior. If a reference to const is bound to a non-const object (const int& r1 = n1;), writing via const_cast<int&>(r1) = 2; is allowed because n1 itself is not const, but doing so hides the read-only intent of the interface.
  • Forgetting the same split on container iterators: In standard containers such as std::vector, const_iterator is an iterator to const value_type—just like const T*, the iterator can move, but it cannot modify the element it points to. For more on how vector iterators behave, see What are std::vector reallocation and iterator invalidation? Why a crash follows push_back.
  • Choosing among C++ casts: For when to use each C++ cast operator in general, see “static_cast vs reinterpret_cast: how do you choose a C++ cast?”.

What are the frequently asked questions?

One-line answer: Here are the rules for const after a member function parameter list, the difference between const and constexpr, and how the same rule applies to multi-level pointers like int* const*.

What does const at the end of a member function mean?

According to cppreference’s non-static member functions page, a member function declared with const after its parameter list makes *this const-qualified inside the function (so the this pointer has type const X*). As a result, it cannot modify member variables normally, and on a const object only const member functions can be called normally. Member functions with different cv-qualifier sequences have different types and may overload each other; for how overloading and overriding differ, see “Overloading vs overriding”.

What is the difference between const and constexpr?

A constexpr specifier used in an object declaration implies const and requires the variable to be initialized by a constant expression, whereas a plain const variable may be initialized with a runtime value. For details, see C++ constexpr: why compute at compile time (Korean).

Does the same rule apply to a pointer to a pointer?

Yes, the left/right-of-* rule repeats at each pointer level. In the cppreference example int* const* npp = &np;, reading from npp leftward gives a non-const pointer (*) to a const pointer (* const) to a non-const int.

One-line answer: These related posts cover pointer memory layout, constexpr, std::vector iterator invalidation, C++ casts, and overriding.

What are the sources?

One-line answer: This post references four cppreference documentation pages on cv qualifiers, pointer declarations, const_cast, and member functions, along with measurements on Linux g++.