liveBook · Manning

Array

As explained in Modern C, Third Edition

An array is an aggregate object containing a fixed sequence of items of the same type, accessed by numeric indices.

`double a[4];` declares one array object containing four ordered subobjects of type `double`. Brackets after the identifier specify the number of elements; `a[0]` names the first element and can be used as a `double` object. Indices for this array run from 0 to 3. section 6.1.1 section 2.2

Reading nested declarations

`double C[M][N];` declares `M` elements, each an array of `N` doubles. Read the declaration from the identifier outward: `C[0]` is an array, while `C[0][0]` is a `double` object. An array’s element type can itself be an array. section 6.1.1

TAKEAWAY: An array is an object, not a pointer or a value that can be manipulated wholesale.

  • Arrays cannot be assigned to or compared, and arithmetic is not defined on arrays themselves. section 6.1.2
  • An array used as a condition evaluates to true because of array decay; this does not make the array a pointer object. section 6.1.2 section 6.1
  • Designated initializers use brackets to select positions when initializing elements. section 6.1.1

Pointers and arrays meet at function interfaces: C rewrites array parameters as pointer parameters, and both forms support bracket notation for element access. Pointer addition can then visit successive elements. Keep the distinction: a pointer does not retain an array object’s length, so a function processing elements through a pointer needs the length supplied separately. section 11.3 section 11.1.2

double A[7] = { 0, 1, 2, 3, 4, 5, 6, };
double s0_7 = sum0(7, &A[0]);    // For the whole
double s1_6 = sum0(6, &A[1]);    // For the last 6
double s2_3 = sum0(3, &A[2]);    // For the 3 in the middle

These calls pass an element’s address and an explicit count to process the whole array or a selected portion of it. section 11.1.2

How Modern C, Third Edition covers it

How deep it goes
Explains declarations, nested arrays, element access, and operation restrictions; develops pointer relationships separately.
Examples
Uses double-array declarations, nested indexing, and array portions passed to a summing function with explicit lengths.
What it assumes
Assumes familiarity with C declarations and objects; introduces array subobjects before developing the pointer relationship.
What you can do after
Apply C array declarations and indexing while avoiding whole-array assignment, comparison, and assumptions that pointers preserve array lengths.
How it connects
Connects arrays to aggregate types, multidimensional arrays, designated initializers, pointers, array decay, and function parameters.

Also explained in

Compare how 40 books explain array →

Related concepts

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