### Arrays

An array is an ordered group of data objects. Each object is called an element. All elements within an array have the same data type.

Use any type specifier in an array definition or declaration. Array elements can be of any data type, except function or, in C++, a reference. You can, however, declare an array of pointers to functions.

The array declarator contains an identifier followed by an optional subscript declarator. An identifier preceded by an * ( asterisk) is an array of pointers.

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

- Array definitions that contain initializations
- extern declarations
- Parameter declarations.

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.

An unsubscripted array name (for example, region instead of region[4]) represents a pointer whose value is the address of the first element of the array, provided the array has previously been declared. An unsubscripted array name with square brackets (for example, region[]) is allowed only when declaring arrays at file scope or in the argument list of a function declaration. In declarations, only the first dimension can be left empty; you must specify the sizes of additional dimensions.

Whenever an array is used in a context (such as a parameter) where it cannot be used as an array, the identifier is treated as a pointer. The two exceptions are when an array is used as an operand of the sizeof or the address (&) operator.

Lexical Elements - Constants

Pointers
Declarators
Initializers
Initializing Arrays
Syntax of a Subscript Declarator