Your browser doesn't support JavaScript Arrays – Windows Programming

Arrays

An array is a data structure consisting of a collection of elements, or objects, of the same data type. Each element in an array occupies its own storage location and is identified by an array index, also called a subscript.

Array indexing in C starts at 0. Therefore, the lowest index identifies the first element, while the highest index identifies the last element.

Declare an array

The declaration statement for an array is similar to a variable declaration with the exception that the number of array elements is specified inside square brackets after the array name –

int example[5];

The declaration above creates an array named example containing five int elements. The compiler allocates enough contiguous memory to store all five elements.

An element is accessed by indexing the array name. This is done by placing the element index within square brackets after the array name. These indexes are called zero-based because the first element in an array is at index 0.

example[0] = 10;   // first element
example[1] = 20;   // second element
example[4] = 50;   // fifth and last element

The number inside the square brackets when accessing an array element is called the array index or subscript.

Initialising Arrays

Arrays can be initialised when they are first declared by use of the equal sign followed by a list of values enclosed in braces and separated by commas:

int example[5]= { 10, 20, 30, 40, 50 };

In the example above, the value 10 is assigned to example[0], the value 20 is assigned to example[1], the value 30 is assigned to the example[2], the value 40 is assigned to example[3]  and the value 50 is assigned to example[4].  If the array size is omitted, the compiler creates an array large enough to hold the initialisation values. The following statement would have the same effect as the previous array declaration statement:

int example[] = { 10, 20, 30, 40, 50 };

Although the number of initialisation values can be fewer than the number of array elements, too many initialisers ie more initialisers than array elements will cause the compiler to generate an error. Any array element not initialised will contain an unknown value.

An empty initialiser list is also permitted in some newer versions of C, but for traditional C code it is preferable to explicitly provide the required initialisation values.

Multidimensional array

A multidimensional array is an array with more than one dimension and therefore requires more than one subscript to identify an element. A two-dimensional array has two subscripts, a three-dimensional array has three subscripts, and so on.

C does not specify a fixed maximum number of array dimensions. In practice, however, the size of an array is limited by the available memory and other implementation constraints.

int example[8][8];

Multidimensional arrays can also be initialised by assigning array elements in order. For
example:

int array[2][4] = { 1, 2, 3, 4, 5, 6, 7, 8 };

which can also be written

array[2][4]={{1,2,3,4},{5,6,7,8}};

results in the following assignments:

array[0][0]=1
array[0][1]=2
array[0][2]=3
array[0][3]=4
array[1][0]=5
array[1][1]=6
array[1][2]=7
array[1][3]=8