Array Declarations

An “array declaration” names the array and specifies the type of its elements. It may also define the number of elements in the array. A variable with array type is considered a pointer to the type of the array elements.

Syntax

declaration : declaration-specifiers init-declarator-list opt ;

init-declarator-list :
init-declarator
init-declarator-list
, init-declarator

init-declarator :
declarator
declarator
= initializer

declarator :
pointer opt direct-declarator

direct-declarator :
direct-declarator [ constant-expression opt ]

Because constant-expression is optional, the syntax has two forms:

The first form defines an array variable. The constant-expression argument within the brackets specifies the number of elements in the array. The constant-expression, if present, must have integral type, and a value larger than zero. Each element has the type given by type-specifier, which can be any type except void. An array element cannot be a function type.

The second form declares a variable that has been defined elsewhere. It omits the constant-expression argument in brackets, but not the brackets. You can use this form only if you previously have initialized the array, declared it as a parameter, or declared it as a reference to an array explicitly defined elsewhere in the program.

In both forms, direct-declarator names the variable and may modify the variable's type. The brackets ([ ]) following direct-declarator modify the declarator to array type.

Type qualifiers can appear in the declaration of an object of array type, but the qualifiers apply to the elements rather than the array itself.

You can declare an array of arrays (a “multidimensional” array) by following the array declarator with a list of bracketed constant expressions in this form:

type-specifier declarator[constant-expression] [constant-expression]...

Each constant-expression in brackets defines the number of elements in a given dimension: two-dimensional arrays have two bracketed expressions, three-dimensional arrays have three, and so on. You can omit the first constant expression if you have initialized the array, declared it as a parameter, or declared it as a reference to an array explicitly defined elsewhere in the program.

You can define arrays of pointers to various types of objects by using complex declarators, as described in “Interpreting More Complex Declarators”.

Arrays are stored by row. For example, the following array consists of two rows with three columns each:

char A[2][3];

The three columns of the first row are stored first, followed by the three columns of the second row. This means that the last subscript varies most quickly.

To refer to an individual element of an array, use a subscript expression, as described in “Postfix Operators”. These examples illustrate array declarations:

float matrix[10][15];

The two-dimensional array named matrix has 150 elements, each having float type.

struct {

float x, y;

} complex[100];

This is a declaration of an array of structures. This array has 100 elements; each element is a structure containing two members.

extern char *name[];

This statement declares the type and name of an array of pointers to char. The actual definition of name occurs elsewhere.

Microsoft Specific

The type of integer required to hold the maximum size of an array is the size of size_t. The size_ttype definition for 16-bit targets is an unsigned short, with the range 0x0000 to 0xFFFF hexadecimal. Huge arrays can exceed this limit if they contain more than 65,535 elements or the size of the element multiplied by the number of elements is greater than 65K. Arithmetic operations on arrays specified with the __hugekeyword should therefore cast size_tand the results of an arithmetic operation on pointers to unsigned long.¨32-Bit Specific

For 32-bit targets, size_t is unsigned long and the __huge keyword is not required.¨

Storage of Arrays

The storage associated with an array type is the storage required for all of its elements. The elements of an array are stored in contiguous and increasing memory locations, from the first element to the last.