Arrays

On this course, it is common to use the STL structure vector when several values of the same type need to be stored and the elements then accessed by their index number. However, there is a more primitive type in C++ that acts partly like a vector. This structure is called an array.

An array is a very simple static data structure (meaning it has a fixed number of elements), and you can access its separate elements with the [] operator. C++ inherits this structure directly from C, the ancestor of C++.

On this course, you will not need arrays themselves, because all the necessary things are easier to do with vector, but for your general education, it is good to know something about how the array works. You will need this information if you ever need to write programs with C.

The array type variable is defined like this:

int numbers[3];  // The array has space for three integers

As a result of the definition above, the compiler allocates enough consequent memory locations to store three int values, one after another.

../../_images/muisti_06_en.png

You can now use the [] operator to access the elements of the array:

numbers[0] = 6;
numbers[1] = 12;
numbers[2] = 24;
cout << numbers[0] + numbers[2] << endl;  // 30

and after that, the memory looks like this:

../../_images/muisti_07_en.png

For efficiency reasons, the array type has been implemented so that it can be presented as a constant pointer to its first element.

Therefore, the code

cout << numbers << endl;

will print the address 0x000004 in the case of the exemplary array above.

Since the array is always understood as an address to its first element, it is a rather primitive data type:

  • An array variable cannot be assigned into another array variable with the operator =. You cannot initialize an array with another array either.
  • If you give an array as a parameter to a function, the array will always act like a reference parameter, because the function receives the memory address of the array’s first element as the formal parameter. When this address is handled with the [] operator, we naturally end up operating with the elements of the original array.
  • An array cannot directly be an element in an STL container.

Instead, you can go through the array with a pointer:

int* array_ptr = nullptr;
array_ptr = numbers;
while ( array_ptr < numbers + 3 ) {
    cout << *array_ptr << endl;
    ++array_ptr;
}

Above, we applied the so-called pointer arithmetic: You can add an integer into the starting address of the array, and the result is a pointer to an element whose index is the mentioned integer.

If the added value is the number of elements in the array, the result is a pointer that points to the memory location immediately after the last element:

cout << numbers + 3 << endl;  // Prints memory address 16

It is handy to use this address in the condition of a loop that uses a pointer to go through an array, just like we did above.

In practice, pointer arithmetic is also the reason why the code

cout << numbers[2] << endl;
numbers[1] = 99;

means the same as the code

cout << *(numbers + 2) << endl;
*(numbers + 1) = 99;

As we said earlier, arrays and their functionalities are not among the most essential content of this course. However, it is worth knowing them for the future. It is not completely unexpected that you end up writing some lower lever C code on the later courses (e.g. Microprocessors, Machine-Level Programming), where you will encounter arrays.