Next: , Previous: , Up: Arrays   [Contents][Index]

16.5 Incomplete Array Types

An array is equivalent, for most purposes, to a pointer to its zeroth element. When that is true, the length of the array is irrelevant. The length needs to be known only for allocating space for the array, or for sizeof and typeof (see Auto Type). Thus, in some contexts C allows

These declarations are examples of incomplete array types, types that are not fully specified. The incompleteness makes no difference for accessing elements of the array, but it matters for some other things. For instance, sizeof is not allowed on an incomplete type.

With multidimensional arrays, only the first dimension can be omitted. For example, suppose we want to represent the positions of pieces on a chessboard which has the usual 8 files (columns), but more (or fewer) ranks (rows) than the usual 8. This declaration could hold a pointer to a two-dimensional array that can hold that data. Each element of the array holds one row.

struct chesspiece *funnyboard[][8];

Since it is just a pointer to the start of an array, its type can be incomplete, but it must state how big each array element is—the number of elements in each row.

Next: , Previous: , Up: Arrays   [Contents][Index]