C Reference ยท Operators and Expressions
The subscript operator [] in C
Reference for the C subscript operator: a[i] as *(a + i), array decay, multidimensional arrays, negative indices, bounds and the i[a] curiosity.
The subscript operator a[i] selects the element at index i. In C it is defined in terms of pointer arithmetic: a[i] means exactly *(a + i). The array a is first converted ("decays") to a pointer to its first element, i elements are added to that pointer, and the result is dereferenced.
Because of this definition, subscripting works on any pointer, not only on arrays, and C performs no bounds checking: an index outside the object is undefined behavior.
int v[4] = {10, 20, 30, 40}; int second = v[1]; /* 20 */ int same = *(v + 1); /* also 20 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
postfix-expression [ expression ]
| Part | Description |
|---|---|
postfix-expression |
A pointer type (an array name is converted to one). |
expression |
An integer expression. By the definition of +, the two operands may be swapped. |
Result: an lvalue of the element type, equal to *(E1 + E2).
Errors / undefined behavior: the pointer arithmetic is only defined while the result stays within the array object or one past its end, and the dereference only within the array itself. a[n] for an array of n elements is undefined behavior to read or write, though &a[n] is a valid one-past-the-end pointer. An index of a pointer with no array behind it, or a null pointer, is also undefined.
Usage notes
- No bounds checks. Reading or writing past the end may appear to work, crash, or silently corrupt data. Pass lengths along with arrays and check them yourself.
- Negative indices are legal when the pointer sits inside an array (
mid[-1]above).v[-1]on the array itself is out of range. - Index type. Use
size_tfor sizes and counts; a signedintindex is fine for small arrays but mixing signed and unsigned types can produce surprising comparisons. - Multidimensional arrays are arrays of arrays in row-major order.
m[r][c]is*(*(m + r) + c). When you pass one, every dimension except the first must be given in the parameter type. - Pointer to pointer is not a 2-D array.
int **ppindexes through separate row pointers and has different memory layout fromint m[R][C]; they are not interchangeable. - Evaluation order. The index expression is a full operand:
a[i++]is fine, buta[i] = i++is undefined behavior (unsequenced, see sequence points). sizeofand decay. Inside a function, an array parameter is a pointer, sosizeofof it gives the pointer size. Compute the length where the array was declared.- Variable length arrays (optional since C11) subscript the same way; see VLAs.
- Compound literals can be subscripted:
(int[]){1, 2, 3}[1]is 2.