What is the difference between const int*, const int * const, and int * const in C++?
In C++, the placement of const
with respect to pointers and pointees can be a bit confusing. Let’s break down these three common declarations and see what they mean in terms of what you can and cannot modify.
1. const int*
(or equivalently int const*
)
- Meaning: A pointer to a constant integer.
- Interpretation: The data being pointed to is read-only (can’t be modified through this pointer), but the pointer itself can be changed to point somewhere else.
const int* ptr; // or int const* ptr; int value1 = 10; int value2 = 20; ptr = &value1; // Okay: we can change 'ptr' to point to another address *ptr = 30; // Error: cannot modify the value through 'ptr' ptr = &value2; // Okay to reassign the pointer
- Key Point: “Pointer to constant data”: The pointer can move, but the data it points to is fixed (as seen through this pointer).
2. int * const
- Meaning: A constant pointer to a (non-constant) integer.
- Interpretation: You cannot change the pointer once it has been initialized (i.e., it always points to the same location), but you can modify the data at that location.
int value1 = 10; int value2 = 20; int* const ptr = &value1; // Must initialize immediately ptr = &value2; // Error: can't change 'ptr' once set *ptr = 30; // Okay: can modify the value of value1 through ptr
- Key Point: “Constant pointer to mutable data”: The pointer is stuck, but the data can change.
3. const int * const
(or int const * const
)
- Meaning: A constant pointer to a constant integer.
- Interpretation: You cannot change where the pointer points, and you cannot modify the value at the pointed location via this pointer.
int value1 = 10; int value2 = 20; const int* const ptr = &value1; // Must be initialized ptr = &value2; // Error: can't change the pointer *ptr = 30; // Error: can't modify the data through ptr
- Key Point: “Pointer can’t move and data can’t be changed through it.”
Usage Scenarios
const int*
: When you want to allow the pointer to re-point to different addresses, but you need to make sure no one modifies the pointed data through this pointer.int * const
: When you want a pointer that always points to the same location, but the underlying data can change. Common in embedded or low-level code when the hardware address is fixed.const int * const
: When neither the pointer nor the data should be changed, often found in tightly controlled interfaces or global constants.
Why This Matters for Coding Interviews
Pointer const-correctness is a foundational topic in C++. Interviewers frequently ask about const
usage to check your understanding of memory safety, API design, and intentional restrictions. Mastering these details shows you’re mindful of both code correctness and maintainability.
Further Learning and Practice
If you want more structured guidance on coding interview topics and general C++ best practices, check out the following courses on DesignGurus.io:
-
Grokking the Coding Interview: Patterns for Coding Questions
Recognize common problem-solving patterns tested at top tech companies. -
Grokking Data Structures & Algorithms for Coding Interviews
Dive deeper into the fundamentals of performance, memory management, and algorithmic thinking.
For free insights into coding interviews and system design, you can also watch the DesignGurus YouTube channel.
Key Takeaway:
const int*
— The pointer can move; the pointed data is read-only (through this pointer).int * const
— The pointer is fixed; the pointed data can be modified.const int * const
— Neither the pointer nor the data can be changed (through this pointer).