liveBook · Manning

Indexing

As explained in Modern C

An index is an integer position used to identify an item in an array.

In C, an index is an integer position used to identify an array element. For an array with n elements, indexing starts at 0 and ends at n−1; the index can be understood as the element’s distance from the array’s start. TAKEAWAY: The first element has index 0, and the last element has index n−1. section 2.3

Designated initialization

A designated initializer makes the index explicit: double A[5] = { [0] = 9.0, [1] = 2.9, [4] = 3.E+25, [3] = .00007, }; Here, [4] initializes the last element. The omitted index [2] receives 0.0. Indexing identifies positions; designated initialization selects those positions during array creation. section 2.3

Indexing is also tied to pointer expressions. If a points to the first element of an array, a+i points to its ith element, while *(a+i) accesses that element’s value. C therefore provides equivalent-looking routes through an array, but a pointer is not itself an array and does not reveal the array length. section 11.1.2 section 11.3

Indexing through a pointer

This summation uses pointer arithmetic rather than bracket notation: double sum0(size_t len, double const* a) { double ret = 0.0; for (size_t i = 0; i < len; ++i) ret += *(a + i); return ret; } The length must be passed separately because a pointer cannot reconstruct the array’s length. section 11.1.2

 8   double A[5] = {
 9     [0] = 9.0,
10     [1] = 2.9,
11     [4] = 3.E+25,
12     [3] = .00007,
13   };


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":16}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the indexed initializer and see how each bracketed integer selects an array position. section 2.3

Function parameters add another rule: an array parameter rewrites to a pointer, so array notation and pointer notation can describe equivalent interfaces. With multidimensional parameters, only the innermost dimension rewrites; conventional indexing remains available inside the function. section 11.3.2

How Modern C covers it

How deep it goes
Defines zero-based positions, then explains pointer arithmetic and array-parameter rewriting without reconstructing lengths.
Examples
Uses designated initializers, pointer-based summation, array slices, and matrix multiplication to demonstrate indexed access.
What it assumes
Builds from arrays and integer positions, then develops pointer relationships and parameter rewriting.
What you can do after
Apply it via C array notation, designated initializers, pointer arithmetic, and multidimensional function parameters.
How it connects
Connects array indexing with arrays, pointers, pointer arithmetic, and multidimensional arrays.

Also explained in

Compare how 15 books explain indexing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.