> ## 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 Array Subscript

The array subscript operator (`[]`) is a postfix operator that provides access to an element of an array or a memory location referenced by a pointer. In C, this operator is strictly defined as syntactic sugar for pointer arithmetic followed by indirection (dereferencing).

## Syntax and Pointer Equivalence

```c theme={"dark"}
postfix-expression[expression]
```

The C standard defines the behavior of the `[]` operator through a direct equivalence to the indirection operator (`*`) and the addition operator (`+`). For any two expressions `E1` and `E2`, the expression `E1[E2]` is identical by definition to:

```c theme={"dark"}
(*((E1) + (E2)))
```

## Operand Constraints

To satisfy the type requirements of the underlying pointer arithmetic, the two operands must meet specific criteria:

1. **Pointer Operand:** One operand must be an expression of type "pointer to complete object type". If an array identifier is used, it undergoes standard array-to-pointer decay, yielding a pointer to its first element.
2. **Integer Operand:** The other operand must be of an integer type (e.g., `int`, `size_t`, `ptrdiff_t`).

## Commutativity

Because the underlying addition `(E1 + E2)` is commutative, the order of the operands around the `[]` operator does not affect the evaluated result. If `p` is a pointer and `i` is an integer, the following expressions are functionally and semantically equivalent:

```c theme={"dark"}
p[i]
i[p]
```

While they are parsed differently in the abstract syntax tree—in `p[i]`, `p` is parsed as the `postfix-expression` and `i` as the `expression`, whereas in `i[p]` their syntactic roles are reversed—both evaluate to the exact same pointer arithmetic: `(*((p) + (i)))`.

## Return Value and Type

The evaluation of the `[]` operator yields an **lvalue** (locator value), meaning it designates an object in memory. However, yielding an lvalue does not automatically mean the result can be used on the left side of an assignment; it must specifically be a *modifiable lvalue*.

If the pointer operand points to a `const`-qualified type (e.g., `const int *p`), or if the result of the subscript evaluates to an array type (e.g., `matrix[0]` when accessing a multidimensional array like `int matrix[3][3]`), the resulting lvalue is not modifiable. Attempting to assign to it will result in a compilation error.

The type of the result is the type pointed to by the pointer operand. For example, if `E1` decays to a pointer of type `int *`, the result of `E1[E2]` is of type `int`.

## Multidimensional Arrays (Chaining)

C does not have a native multidimensional subscript operator. Instead, multidimensional array access is achieved by chaining the `[]` operator. Because the operator has left-to-right associativity, an expression like `E1[E2][E3]` is evaluated as `(E1[E2])[E3]`.

Applying the pointer equivalence rule recursively demonstrates the underlying memory access pattern:

```c theme={"dark"}
// Step 1: Evaluate the outer subscript
(*( E1[E2] + E3 ))

// Step 2: Evaluate the inner subscript
(*( (*(E1 + E2)) + E3 ))
```

## Precedence and Associativity

The `[]` operator resides at the highest level of operator precedence in C, sharing this tier with the function call `()`, structure member `.`, and structure pointer `->` operators. It associates from **left to right**. This high precedence ensures that in an expression like `*p[i]`, the subscript operator is evaluated before the unary dereference operator, parsing as `*(p[i])` rather than `(*p)[i]`.

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