The array declarator contains an identifier followed by an optional subscript declarator. An identifier preceded by an asterisk (*) is an array of pointers.
A subscript declarator has the form:
>>-[--+-+---------------------+--+-----------------------+-----+--]--> | '-type_qualifier_list-' '-assignment_expression-' | +-static--+---------------------+--assignment_expression-+ | '-type_qualifier_list-' | +-type_qualifier_list--static--assignment_expression-----+ '-+---------------------+--*-----------------------------' '-type_qualifier_list-' >--+-------------------------------+--------------------------->< | .---------------------------. | | V | | '---[--constant_expression--]-+-'
where constant_expression is a constant integer expression, indicating the size of the array, which must be positive.
If the declaration appears in block or function scope, a nonconstant
expression can be specified for the array subscript declarator, and the array
is considered a variably modified type. An asterisk within the brackets
of the array subscripting operator indicates a variable length array of
unspecified size. In this case, the array is considered a variably
modified type that can only be used in functions declarations that are not
definitions (that is, in declarations with function prototype scope).
The subscript declarator describes the number of dimensions in the array and the number of elements in each dimension. Each bracketed expression, or subscript, describes a different dimension and must be a constant expression.
The following example defines a one-dimensional array that contains four elements having type char:
char list[4];
The first subscript of each dimension is 0. The array list contains the elements:
list[0] list[1] list[2] list[3]
The following example defines a two-dimensional array that contains six elements of type int:
int roster[3][2];
Multidimensional arrays are stored in row-major order. When elements are referred to in order of increasing storage location, the last subscript varies the fastest. For example, the elements of array roster are stored in the order:
roster[0][0] roster[0][1] roster[1][0] roster[1][1] roster[2][0] roster[2][1]
In storage, the elements of roster would be stored as:
* * * *---------------*---------------* --------------* ^ ^ ^ * * * roster[0][0] roster[0][1] roster[1][0]
You can leave the first (and only the first) set of subscript brackets empty in
In array definitions that leave the first set of subscript brackets empty, the initializer determines the number of elements in the first dimension. In a one-dimensional array, the number of initialized elements becomes the total number of elements. In a multidimensional array, the initializer is compared to the subscript declarator to determine the number of elements in the first dimension.
Related References