> ## 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.

# C Pointer to Const

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 (`*`).

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
#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.

```c theme={"dark"}
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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
