### 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. You can, however, declare an array of pointers to functions.

Declaring an Array

identifier | The name of the array. If preceded by an * (asterisk), the array is an array of pointers.
constant expression | Positive integer expression describing the number of elements in a given dimension of the array. An array can have more than one dimension.

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]

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 (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 been previously 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, and you must specify the sizes of any additional dimensions declared.

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 only exceptions are when an array is used as an operand to the sizeof expression or with an address (&) operator.

Initializing Arrays
The initializer for an array contains the = symbol followed by a comma-separated list of constant expressions enclosed in braces ({ }). You do not need to initialize all elements in an array. Elements that are not initialized (in extern and static definitions only) receive the value 0 of the appropriate type. You cannot have more initializers than the number of elements in the array.

The initializer must be a constant expression if the structure has static storage duration or if you are compiling your source code in ansi mode.

Note: Array initializations can be either fully braced (with braces around each dimension) or unbraced (with only one set of braces enclosing the entire set of initializers). Avoid placing braces around some dimensions and not around others.

Initializing a one-dimensional character array
Initialize a one-dimensional character array by specifying:

- A brace-enclosed comma-separated list of constants, each of which can be contained in a character
- A string constant. (Braces surrounding the constant are optional.)

Initializing a string constant places the null character (\0) at the end of the string if there is room or if the array dimensions are not specified.

Initializing a multidimensional array
Initialize a multidimensional array by:

- Listing the values of all elements you want to initialize, in the order that the compiler assigns the values. The compiler assigns values by increasing the subscript of the last dimension fastest. This form of a multidimensional array initialization looks like a one-dimensional array initialization. The following definition completely initializes the array `month_days`:

         static month_days[2][12] =
         {
          31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
          31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
         };

- Using braces to group the values of the elements you want initialized. You can put braces around each element, or around any nesting level of elements. The following definition contains two elements in the first dimension. (You can consider these elements as rows.) The initialization contains braces around each of these two elements:

         static int month_days[2][12] =
         {
          { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
          { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
         };

- Using use nested braces to initialize dimensions and elements in a dimension selectively.

Constants - String Literals
Declarators
Initializers

Examples of Array Declaration and Use

Array Subscript
Pointers