C++ · Module 7 · Lesson 51 min read

const with Pointers & References

The four combinations

DeclarationRead right-to-leftChange the value?Re-point?
int* pp is a pointer to int✅✅
const int* p (= int const* p)p is a pointer to const int❌✅
int* const pp is a const pointer to int✅❌
const int* const pconst pointer to const int❌❌

Rule: const applies to what's on its left (or the right, if nothing is on the left).

Why pointer-to-const matters

c
void print(const int* data, std::size_t n);   // promises not to modify data

Callers can pass const arrays, and the compiler stops accidental writes.

const references

const T& — read-only access without copying. The most common parameter type for big objects.

Conversions

  • int* → const int* ✅ automatic (adding a promise)
  • const int* → int* ❌ needs const_cast (and writing through it to a truly const object is undefined behaviour)

const objects and member functions

A const object can only call member functions marked const (Unit 8.5):

csharp
double get_balance() const;

"const correctness" ✅

Mark everything const that shouldn't change:

  • parameters: const std::string&
  • member functions that don't modify the object
  • local variables computed once It documents intent and lets the compiler catch mistakes.

constexpr pointers

constexpr const char* NAME = "App"; — a compile-time constant pointer to constant chars.

String literals

"hello" has type const char[6]. Assigning it to char* is an error in C++ (allowed in C).

Example

C++
#include <iostream>

void print_all(const int* data, int n) {        // read-only access
    for (int i = 0; i < n; ++i) std::cout << data[i] << ' ';
    // data[0] = 5;                              // error: assignment of read-only location
    std::cout << '\n';
}

void double_all(int* const data, int n) {      // can modify values, can't re-point data
    for (int i = 0; i < n; ++i) data[i] *= 2;
    // data = nullptr;                           // error: assignment of read-only parameter
}

int main() {
    int a = 1, b = 2;

    const int* p1 = &a;      // pointer to const int
    p1 = &b;                 // OK: re-point
    // *p1 = 5;              // error: value is read-only through p1
    std::cout << "*p1 = " << *p1 << '\n';

    int* const p2 = &a;      // const pointer to int
    *p2 = 50;                // OK: change the value
    // p2 = &b;              // error: the pointer itself is const
    std::cout << "a = " << a << '\n';

    const int* const p3 = &b;   // nothing can change through p3
    std::cout << "*p3 = " << *p3 << '\n';

    int numbers[] = { 3, 6, 9 };
    double_all(numbers, 3);
    print_all(numbers, 3);       // int* converts to const int* automatically

    const int limits[] = { 100, 200 };
    print_all(limits, 2);
    // double_all(limits, 2);    // error: can't pass const int* where int* is expected

    const char* greeting = "Namaste";   // string literals are const
    std::cout << greeting << " has first letter " << *greeting << '\n';
    return 0;
}

/* Output:
*p1 = 2
a = 50
*p3 = 2
6 12 18
100 200
Namaste has first letter N
*/