Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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 (*).
const int *ptr1;  // Preferred by convention
int const *ptr2;  // Syntactically identical
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.
int val1 = 10;
int val2 = 20;

const int *ptr = &val1;

int read_val = *ptr; // PERMITTED: Reading the value
ptr = &val2;         // PERMITTED: Reassigning the pointer to a new address

// *ptr = 30;        // ILLEGAL: Error - assignment of read-only location

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.
int mutable_var = 42;
const int *ptr = &mutable_var;

mutable_var = 100;   // PERMITTED: The original variable is not const
// *ptr = 100;       // ILLEGAL: Cannot mutate through the pointer-to-const

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.
#include <stddef.h>

// The const qualifier guarantees the memory 'str' points to will not be mutated
size_t string_length(const char *str) {
    size_t len = 0;
    while (*str != '\0') {
        len++;
        str++; // PERMITTED: Modifying the pointer itself to traverse memory
        // *str = 'A'; // ILLEGAL: Cannot modify the underlying characters
    }
    return len;
}

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.
int val = 10;

// Pointer to Const
const int *ptr1 = &val;
ptr1++;       // Allowed
// *ptr1 = 5; // Error

// Const Pointer
int * const ptr2 = &val;
// ptr2++;    // Error
*ptr2 = 5;    // Allowed
Master C with Deep Grasping Methodology!Learn More