C++ Arrays

Read time: 12 minutes (3087 words)

C++, like most other programming languages, supports the building of a data structure that can hold a number of identical objects. This objects can be standard data items, or objects you create with classes. When you set up an array, you must decide how big it will be when you compile your program. It cannot grow later. There are ways to solve this limitation, but we will explore them later!

Here is a simple array declaration:

int grades[5] = { 99, 87, 53, 75, 95 };

The square brackets after the name grades in this case, tell the system to set up exactly 5 containers. We will access those containers by using the array name, and providing a subscript which can be a literal number, or a variable whose value is in the limits of the array size.

Warning

It is a HUGE mistake to ask for grades[5], even though these are 5 items defined. The first subscript, or index is zero, so the last one is one less than the number used to set the array up!

Your code can access a container outside the defined range, but doing so is asking for trouble! Don’t do that, even by accident!

Rather than use literal numbers to specify the size of an array, it is far better to declare a constant integer, and use that:

const int MAXSIZE = 5;

int grades[MAXSIZE];

This code set up exactly 5 grade slots, but left them uninitialized. That is fine as long as you remember that fact!

std::cout << grades[2];  // expect nonsense here!

Array Concerns

Arrays are handy, but there are several things you need ot be thinking about when you use them.

Arrays are Fixed at Compile Time

You must decide how big your array is at compile time, not at run time. In order to let the user decide how much data we can handle, we need another way to set up an array, and that is a topic for later in the course!

Arrays as Parameters

When you pass an array to a function (or method) C++ always send down a “reference” to that array. The function has no idea how big the array is, so you need to provide a parameter to tell it that critical piece of information.

Arrays only Hold One Data Type

If you started working in Python, it will be annoying to find out that once you set up an array, you cannot decide to put another data type in the container, even though Python allows this. C++ is called strongly typed and enforces the type declarations you make in your code. Fortunately, you can easily set up an array of any kind of object you like

Arrays of Objects

Now that we know about classes, we might find it handy to have an array of objects (like poolballs for example).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

#define MAX_SIZE 5

class SimpObj {
    public:
        int value;
};

int main() {
    SimpObj objs[5];
    int simple_ints[6] = { 5, 4, 3, 2, 1, 0 };

    // initialize the array objects
    for( int i=0; i< MAX_SIZE; i++ ) 
        objs[i].value = simple_ints[i];

    for(int i=0; i<6;i++) {
        cout << simple_ints[i] << "=";
        cout << objs[i].value << " ";
    }
    cout << endl;

    for(int i=0; i< MAX_SIZE; i++)
        cout << objs[i].value - i << " ";
    cout << endl;
}

The output from this test is a bit surprising:

$ ./test
5=5 4=4 3=3 2=2 1=1 0=32767
5 3 1 -1 -3

Look closely at the code. I over-indexed the arrays on the output loop. Everything looked fine up until we went past the actual last point in the two arrays. Then we stepped on ourselves. The second line should have been identical to the first (minus the extra output from our objects), but the values got smashed.

Welcome to array indexing issues. You need to be careful when accessing arrays, or this kind of problem can lead to much lost time trying to figure out what happened!

This example should get you started with this.

We will use arrays a lot (you might notice we already have!). Other topics associated with arrays will come up as we need to discuss them. For now, they should feel pretty familiar from your Python days, (with that one restriction on data type!)