# const pointer vs pointer to const, and const correctness

> Explain the difference between 'const int p', 'int const p' and 'const int const p'. What is const correctness and what does a const member function mean?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-const-correctness
Last updated: 2026-11-28

Difficulty: Beginner

## Question

Explain the difference between 'const int\* p', 'int\* const p' and 'const int\* const p'. What is const correctness and what does a const member function mean?

## Answer

If you have ever stared at a declaration like 'const int\* const p' and felt your eyes glaze over, you are in good company. The good news is there is a mechanical trick that makes all of them easy, and interviewers love checking whether you know it.

The trick is to read the declaration right to left, or more precisely: whatever is immediately to the left of the const is what is constant (and if const is the leftmost token, it applies to the thing right after it). So 'const int\* p' reads as 'p is a pointer to an int that is const': you cannot modify the int through p, but you can point p somewhere else. 'int const\* p' means exactly the same thing. Now 'int\* const p' reads 'p is a const pointer to an int': p itself is fixed for life and must be initialised, but you can modify the int it points at. And 'const int\* const p' is both: neither the pointee nor the pointer can change.

An everyday analogy: imagine a hotel key card. A pointer to const is a card that lets you look into a room but not rearrange it, although you can swap the card for a different room's card. A const pointer is a card permanently tied to one room, where you may rearrange the furniture. The last form is a permanent view-only card.

Const correctness is the discipline of marking everything that should not change as const, so the compiler enforces your intent. It matters because it documents contracts (this function will not touch your data), enables binding temporaries to const references, allows the compiler to catch bugs early, and lets const objects be used at all: you can only call const member functions on a const object.

A const member function, declared as 'int size() const', promises not to modify the object's observable state: inside it, 'this' has type 'const T\*', so assigning to normal data members is a compile error. Getters should almost always be const. If a member genuinely needs to change inside a const function (a cache or a mutex), mark that member mutable. Overloading on constness is also legal and used by containers: operator\[\] has one version returning T& and a const version returning const T&.

Edge cases interviewers probe: const_cast can strip constness, but modifying an object that was originally defined const through it is undefined behaviour. Also, a top-level const on a by-value parameter ('void f(const int x)') is not part of the function signature, whereas const on the pointee ('const int*') is. And a string literal has type const char\[N\] in C++, so 'char* s = "hi"' is ill-formed.

## Code examples

### Three flavours of const with pointers

```
#include <iostream>

class Counter {
    int n_ = 0;
    mutable int reads_ = 0;
public:
    void inc() { ++n_; }
    int get() const { ++reads_; return n_; }  // reads_ is mutable
    int reads() const { return reads_; }
};

int main() {
    int a = 10, b = 20;

    const int* p1 = &a;      // pointer to const int
    p1 = &b;                 // OK: pointer can move
    // *p1 = 5;             // error: cannot modify through p1

    int* const p2 = &a;      // const pointer to int
    *p2 = 5;                 // OK: modify pointee
    // p2 = &b;             // error: pointer is fixed

    const int* const p3 = &a;  // neither can change
    std::cout << *p1 << " " << *p2 << " " << *p3 << "\n";

    const Counter c;
    std::cout << c.get() << " " << c.reads() << "\n";
}
```

p1 now points to b (20). a was changed to 5 through p2. On the const Counter, only const member functions are callable; the mutable counter reads\_ still updates.

## Key points

- Read declarations right to left: const applies to what is on its left (or right, if leftmost).
- const int\* and int const\* are identical; int\* const is a different thing.
- A const member function has 'const this' and cannot modify non-mutable members.
- Use mutable only for logically-const state like caches or locks.
- Modifying an originally-const object through const_cast is undefined behaviour.

## Common follow-up questions

- Can a const object call a non-const member function?
- What is the difference between const and constexpr?
- Why is 'char\* s = "hello"' ill-formed in C++?

## Interview tips

- Write all three declarations on the whiteboard and annotate each with 'pointer can move? data can change?'.
- Bring up mutable and const overloads of operator\[\] to show real-world awareness.

## Concepts covered

const, pointers, const member functions, const correctness

Previous: [References vs pointers in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-references-vs-pointers) · Next: [Stack vs heap memory in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-stack-vs-heap) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [new/delete vs malloc/free](https://www.internhack.in/learn/interview/cpp-interview/cpp-new-delete-vs-malloc-free)
- [Memory leaks, dangling pointers and double free](https://www.internhack.in/learn/interview/cpp-interview/cpp-memory-leaks-dangling-pointers)
- [RAII: Resource Acquisition Is Initialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-raii)
- [Constructors in C++: default, copy and move](https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move)
- [Shallow copy vs deep copy](https://www.internhack.in/learn/interview/cpp-interview/cpp-shallow-vs-deep-copy)
- [Rule of three, rule of five and rule of zero](https://www.internhack.in/learn/interview/cpp-interview/cpp-rule-of-three-five-zero)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
