Static arrays

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Static arrays

What is a static array

A static array is a group of variables of the same type that are stored next to another in the memory. Once defined, the size of a static array can not change (That is why it is called static array). The variables inside a static array does not have a name. Instead, they are accessed using their index inside the array (Starting at the index 0).

Defining a static array

We define a static array the same way we would define a basic variable, except that we add the number of elements (variables) inside the array between brackets [] after the variable name. Example of a definition of a static array of type int with a size of 10 elements:

int array[10];

We can set the value of the first element (variable), inside the array, like that:

array[0] = 5;

And the last element:

array[9] = 90;

Remember, the first index is 0, so the last one is the size of the array minus 1.

Setting the values at the definition of the array

We can define the values inside a static array at its definition by enumerating them (Separated by commas) inside braces {}. Example:

float array[3] = {9, 3, 15.3};

When we define the values, specifying the size of the array is not required (The compiler will calculate its size). The following works:

int array[] = {1, 2, 3, 4, 5}; // The size of the array is 5

Multi-dimensional array

It is possible to define static arrays with many dimensions. For example, an array of array would be a 2 dimensional array. To define a 2D array, we must add 2 sets of brackets [][] after the name of the array, in its definition. The number in the first set of brackets represent the number of rows and the second represent the number of columns in the 2 dimensional array. Example of a 2 by 6 (2x6) array:

int array2d[2][6];

We can access the element at the second row and fourth column like that:

array2d[1][3] = 100;

A 2x6 array contains the same amount of elements as a single dimension array with a size of 12 (And in the computer memory, both arrays are stored the same way). The only difference is in the way we access the elements.

We can define arrays with as many dimensions we want. Example of a 4 dimensional array:

int array4d[2][2][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

As you can see, the values of multi-dimensional arrays can also be set at their definition using the braces. However, when doing so, the size of all dimensions, except the first one, must be specified. For example, the following works:

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

The first dimension of the array will be set to 2. The code below would also work:

int array3d[][2][2] = {1, 2, 3, 4, 5, 6};

The first dimension would also be set to 2. However, the last two elements in the array would not have their value set.

Both the following definitions of static arrays would cause an error:

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

Note that the following code:

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

Is internally, by the compiler, converted to:

int array[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; array[(2*3) + 1] = 84;

What to remember is that single and multi dimensional arrays are basically the same. The only real difference is in the way to use them.

Constant arrays

Static arrays can also be defined as constant. Example:

const int array[5] = {5, 4, 3, 2, 1};

Without surprise, after its definition, we can use the array to access its values, but we can not change them (So we must set the values of a constant static array at its definition).