Const Pointer

A constant pointer refers to a pointer whose value (i.e., the address it holds) cannot be changed after initialization. This concept can be broken down into different forms based on whether the pointer itself is constant, the data it points to is constant, or both.

There are two types of const when it comes to pointers. const pointers are pointers whose behavior is influenced by the const keyword, either by making the pointer itself constant or the data being pointed to constant.

1️⃣ Pointer to a Constant (const T* ptr):

A pointer to a constant means that the data being pointed to is constant, but the pointer itself is not. This means you cannot modify the value that the pointer is pointing to, but you can change the pointer to point to a different memory location.

Syntax:
const int* ptr;
  • Characteristics:
    • You can change what the pointer points to, but you cannot modify the value it points to through the pointer.
    • The value being pointed to is read-only via this pointer.
    • The pointer itself is modifiable.
  • Practical Uses Cases:
    • Used when you want to protect the data being pointed to from modification but still allow the pointer to change its target.
    • Example: In function parameters to ensure the function does not modify the data.
    • void printValue(const int* ptr) {
          // ptr points to constant data, so you cannot modify *ptr
          std::cout << *ptr << std::endl;
      }
      
Example:
int a = 10;
int b = 20;
const int* ptr = &a;  // Pointer to constant integer (cannot modify 'a' through ptr)

*ptr = 15;  // Error: cannot modify the value of a constant
ptr = &b;   // OK: you can change what the pointer points to

In this case, ptr can point to different integers (a or b), but it cannot be used to change the values of a or b.

2️⃣ Constant Pointer (T* const ptr):

A constant pointer means that the pointer itself is constant, but the data being pointed to is not. This means you cannot change the memory address the pointer is pointing to, but you can modify the value at that address.

Syntax:
int* const ptr;
  • Characteristics:
    • You cannot change what the pointer points to after it has been initialized.
    • You can modify the value of the data the pointer points to.
  • Use Cases:
    • Used when you want to fix the pointer to a specific object but allow the modification of the object itself.
    • Example: To ensure that the pointer always points to a specific object, like a hardware register in embedded systems.
    • int value = 42;
      int* const p = &value;  // Pointer must always point to 'value'
      *p = 100;  // OK
      
Example:
int a = 10;
int* const ptr = &a;  // Constant pointer to an integer

*ptr = 15;  // OK: you can modify the value of 'a'
ptr = &b;   // Error: cannot change the address the pointer points to

In this example, ptr must always point to a after initialization, but the value of a can be modified.

3️⃣ Constant Pointer to Constant Data (const T* const ptr):

A constant pointer to constant data means that both the pointer and the data being pointed to are constant. This means you cannot change where the pointer points to, and you cannot modify the value of the data it points to.

Syntax:
const int* const ptr;
  • Characteristics:
    • You cannot change the address the pointer holds (the pointer is constant).
    • You cannot modify the value of the data the pointer points to (the data is constant).
  • Use Cases:
    • Used when both the pointer and the data it points to should be immutable.
    • Example: When a function or object needs to use a fixed, read-only value and ensure it remains constant.
    • int value = 10;
      const int* const p = &value;
      
Example:
int a = 10;
const int* const ptr = &a;  // Constant pointer to constant integer

*ptr = 15;  // Error: cannot modify the value of a constant
ptr = &b;   // Error: cannot change the address of a constant pointer

Summarizing the Types of Const Pointers

TypeMeaningExample Syntax
Pointer to ConstantThe value pointed to is constant, but the pointer itself can change to point to other data.const int* ptr;
Constant PointerThe pointer itself is constant (it cannot point to a different memory address), but the data it points to can change.int* const ptr;
Constant Pointer to Constant DataBoth the pointer and the value it points to are constant, so neither can be changed.const int* const ptr;