Chapter 8. Arrays

An array contains objects of a given type, stored consecutively in a continuous memory block. The individual objects are called the elements of an array. The elements’ type can be any object type. No other types are permissible: array elements may not have a function type or an incomplete type (see “Typology”).

An array is also an object itself, and its type is derived from its elements’ type. More specifically, an array’s type is determined by the type and number of elements in the array. If an array’s elements have type T, then the array is called an “array of T.” If the elements have type int, for example, then the array’s type is “array of int.” The type is an incomplete type, however, unless it also specifies the number of elements. If an array of int has 16 elements, then it has a complete object type, which is “array of 16 int elements.”

Defining Arrays

The definition of an array determines its name, the type of its elements, and the number of elements in the array. An array definition without any explicit initialization has the following syntax:

type name[ number_of_elements ];

The number of elements, between square brackets ([]), must be an integer expression whose value is greater than zero. Here is an example:

char buffer[4*512];

This line defines an array with the name buffer, which consists of 2,048 elements of type char.

You can determine the size of the memory block that an array occupies using the sizeof operator. The array’s size in memory is ...

Get C in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.