An array is a collection of objects of the same data type, allocated contiguously in memory. Individual objects in an array, called elements, are accessed by their position in the array. The subscripting operator ([]) provides the mechanics for creating an index to array elements. This form of access is called indexing or subscripting. An array facilitates the coding of repetitive tasks by allowing the statements executed on each element to be put into a loop that iterates through each element in the array.

The C and C++ languages provide limited built-in support for an array type: reading and writing individual elements. Assignment of one array to another, the comparison of two arrays for equality, returning self-knowledge of size are not supported by either language.

The type of an array is derived from the type of its elements, in what is called array type derivation. If array objects are of incomplete type, the array type is also considered incomplete. Array elements may not be of type void or of function type. However, arrays of pointers to functions are allowed. C++ Array elements may not be of reference type or of an abstract class type.

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

Read syntax diagramSkip visual syntax diagramArray subscript declarator syntax
   V                           |

The 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 variable-length array, as described in Variable length arrays .

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:


The first subscript of each dimension is 0. The array list contains the elements:


The following example defines a two-dimensional array that contains six elements of type int:


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:


In storage, the elements of roster would be stored as:

 *               *               *

 ^               ^               ^
 *               *               *
 roster[0][0]    roster[0][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.

Related information