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 const pointer in C refers to the application of the const type qualifier to pointer declarations, establishing read-only access paths or immutable memory addresses. The placement of the const keyword relative to the pointer declarator (*) dictates whether the pointer’s stored address, the data accessed through that pointer, or both, are restricted from modification. Crucially, applying const to the data type does not inherently freeze the underlying memory; it only prevents mutation through that specific pointer identifier. To parse these specific declarations accurately, C developers often utilize the right-to-left reading rule: read starting from the identifier, moving left through the declaration. This is a helpful mnemonic specifically for reading simple pointer qualifiers. For complex declarations involving arrays or functions, developers must rely on the “inside-out” or “clockwise/spiral” rule instead.

1. Pointer to Constant Data

The const qualifier applies to the base type. The pointer object can be reassigned to hold a new address, but the data cannot be modified via dereferencing this specific pointer. If the underlying variable is mutable, its value can still be changed directly or through a different, non-const pointer.
void demo_pointer_to_const(void) {
    int value1 = 10;
    int value2 = 20;

    // Read right-to-left: ptr is a pointer (*) to an int that is const
    const int *ptr = &value1; 

    // Equivalent syntax:
    // int const *ptr = &value1;

    ptr = &value2;       // VALID: The pointer's stored address can change
    // *ptr = 30;        // COMPILER ERROR: Cannot modify data through this pointer
    
    value2 = 30;         // VALID: The underlying memory is still mutable directly
}

2. Constant Pointer to Mutable Data

The const qualifier applies directly to the pointer itself. Its stored memory address becomes immutable and must be initialized at the time of declaration. However, the data accessed through the pointer can be modified.
void demo_const_pointer(void) {
    int value1 = 10;
    int value2 = 20;

    // Read right-to-left: ptr is a const pointer (*) to an int
    int * const ptr = &value1; // Must be initialized here

    *ptr = 30;           // VALID: Data accessed through the pointer can be modified
    // ptr = &value2;    // COMPILER ERROR: The pointer's stored address cannot change
}

3. Constant Pointer to Constant Data

The const qualifier is applied to both the base type and the pointer. The pointer’s stored address must be initialized upon declaration and cannot be reassigned, and the data cannot be modified through this pointer. The underlying data itself is not frozen unless it was originally declared as const; if it is a standard mutable variable, it can still be modified directly.
void demo_const_pointer_to_const(void) {
    int value1 = 10;
    int value2 = 20;

    // Read right-to-left: ptr is a const pointer (*) to an int that is const
    const int * const ptr = &value1; // Pointer must be initialized here

    // ptr = &value2;    // COMPILER ERROR: The pointer's stored address cannot change
    // *ptr = 30;        // COMPILER ERROR: Cannot modify data through this pointer
    
    value1 = 30;         // VALID: The underlying variable 'value1' is still mutable
}

Const Correctness in Function Parameters

Applying const to pointer parameters is a fundamental practice known as “const correctness.” By declaring a parameter as a pointer to constant data, the function signature provides a compiler-enforced guarantee that the function will not modify the memory through that specific pointer parameter. It creates a read-only access path. The underlying memory can still be modified if the original variable is mutable and is accessed via an aliased non-const pointer, or if the const qualifier is explicitly cast away.
#include <stdio.h>
#include <stddef.h>

void print_array(const int *arr, size_t size) {
    for (size_t i = 0; i < size; i++) {
        // arr[i] = 0; // COMPILER ERROR: Assignment of read-only location
        printf("%d\n", arr[i]);
    }
}

Pointer Arithmetic Semantics

When a pointer itself is declared as const (Variations 2 and 3), pointer arithmetic (e.g., ptr++, ptr += 2) is strictly prohibited by the compiler, as these operations attempt to mutate the pointer object to hold a new address. If only the underlying data is const (Variation 1), pointer arithmetic remains valid, provided the pointer is traversing a valid array object.
void demo_pointer_arithmetic(void) {
    int arr[3] = {10, 20, 30};

    const int *ptr1 = arr;
    int * const ptr2 = arr;

    ptr1++; // VALID: Modifies the pointer object to hold the address of the next element
    // ptr2++; // COMPILER ERROR: Attempts to modify a const pointer object
}
Master C with Deep Grasping Methodology!Learn More