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.

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

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:
(*((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:
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:
// 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].
Master C with Deep Grasping Methodology!Learn More