Skip to main content
A pointer to a constant in C is a pointer that cannot be used to modify the value of the object it points to. While the pointer itself is mutable and can be reassigned to hold a different memory address, dereferencing the pointer to mutate the underlying data violates its type qualifier and results in a compilation error.

Syntax

There are two syntactically equivalent ways to declare a pointer to a constant. The const qualifier can appear either before or after the base type, as long as it precedes the asterisk (*).
Using the “read right-to-left” rule for C declarations:
  • *ptr1: ptr1 is a pointer…
  • int: …to an integer…
  • const: …that is constant.

Mechanical Behavior

The compiler enforces read-only access through this specific pointer. It dictates what operations are legally permitted on the pointer and its dereferenced value.

Underlying Data Mutability

A critical technical distinction is that a pointer to const does not guarantee the underlying memory is strictly immutable. It only guarantees that the memory cannot be mutated through that specific pointer. If the underlying variable was not declared as const, it can still be modified directly or through a different, non-const pointer.

Function Signature Semantics

The primary structural application of pointers to constants is within function parameters. Declaring a parameter as a pointer to const allows a function to accept data by reference (avoiding the memory and performance overhead of copying arrays or large structures) while establishing a strict, compiler-enforced contract that the function will not modify the original data.

Disambiguation: Pointer to Const vs. Const Pointer

A pointer to const is frequently confused with a const pointer. Their mechanical restrictions are inverted based on the placement of the const keyword relative to the asterisk.
  • Pointer to Const (const int *ptr): The data is read-only through the pointer; the pointer address is mutable.
  • Const Pointer (int * const ptr): The data is mutable through the pointer; the pointer address is read-only.
  • Const Pointer to Const (const int * const ptr): Both the data (through the pointer) and the pointer address are read-only.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More