liveBook · Manning

Array

As explained in Modern C

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

Four elements

int main(void) { double a[4] = { [0] = 2.0, [3] = 5.0 }; a[1] = a[0] + a[3]; return a[1] == 7.0 ? 0 : 1; } The declaration makes one array object containing four double subobjects. Indices 0 through 3 select its elements; a[1] can be read and assigned like a double variable. The bracketed entries in the initializer choose elements by index. section 2.2 section 5.5 section 6.1.1

TAKEAWAY: An array groups objects of one base type into a single object. In double a[4], the brackets in the declaration specify four elements; in a[1], they select one. Keep the array object distinct from its individual elements and their values. section 2.2 section 6.1.1

Length and initialization have separate rules:

  • A fixed-length array gets its length from an integer constant expression or, with empty declaration brackets, from its initializer. The specified length must be positive. section 6.1.3
  • Use braces for an array initializer. Designated initializers such as [3] = 5.0 make the intended position explicit; {0} initializes an array that is not a variable-length array. section 5.5
  • A non-constant length makes a variable-length array. Such an array cannot have an initializer or be declared outside a function. section 6.1.3
   double C[M][N];
   double (D[M])[N];


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":9}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":9}],[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":9}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":9}]]"}
!@%STYLE%@!

Read double C[M][N] from the identifier outward: C has M elements, each an array of N doubles. The parenthesized declaration of D describes the same type. Thus C[0] is an array, while C[0][0] is a double. section 6.1.1

TAKEAWAY: There are array objects but no array values. You cannot assign one whole array to another, do arithmetic on an array itself, or compare arrays as whole values. For an actual array object A, (sizeof A)/(sizeof A[0]) gives its number of elements. section 6.1.2 section 6.1.3

TAKEAWAY: Arrays are not pointers. When an array is evaluated where a value is needed, it decays to a pointer to its first element; A[i] and *(A+i) then express the same access. Function array parameters are rewritten as pointers, so a function receiving one cannot recover the original array length with sizeof: pass the length separately. section 6.1 section 11.3.1 section 11.1.2

How Modern C covers it

How deep it goes
Works through declarations, initializers, lengths, operations, nested arrays, and array decay; defers the full pointer relationship until later.
Examples
Uses double arrays, designated initializers, equivalent multidimensional declarations, and pointer-based functions that sum array elements.
What it assumes
Assumes familiarity with declarations and basic types; introduces arrays before developing their relationship with pointers.
What you can do after
Build it from scratch: declare, initialize, index, and measure arrays while distinguishing an array object from a pointer parameter.
How it connects
Connects arrays to aggregate types, designated initializers, sizeof, variable-length arrays, pointers, and array-to-pointer decay.

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. The section links point to the parts of the book it draws on.