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

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:

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:



Constants - String Literals
Declarators
Initializers


Examples of Array Declaration and Use


Array Subscript
Pointers