C++ arrays do exactly what their name implies. They allow us to handle whole arrays of data in one simple structure. Certainly, there is quite a bit to learn about arrays but actually they are very straight forward as we we will see.
[widgets_on_pages id=”udemy_advert_cpp_1″][widgets_on_pages id=”udemy_code_details”]

What exactly, is an array?

If a variable is a box in which we can store a value of a specific type, like int, float or char, then we can think of an array as a whole row of boxes. The row of boxes can be of almost any size but can be of only one type.

Declaring an array

We can declare an array of 10  int type variables like this:

int aBunchOfInts [10];

Now we have an array called aBunchOfInts that can store 10 int values. At the moment, however, it is empty.

Accessing the elements of an array

To add values to the elements of an array we can use syntax we are already familiar with combined with some new syntax, known as array notation. In this next code, we store the value of 99 into the first element of the array:

aBunchOfInts [0] = 99;

[widgets_on_pages id=”bcgp_cfgp_gpp”]
To store a value of 999 at the second element we write this code.

aBunchOfInts [1] = 999;

We can store a value of 9 at the last element like this.

aBunchOfInts [9] = 9;

Note that the elements of an array always start at zero and go up to the size of the array take away one. Similarly to ordinary variables we can manipulate the values stored in an array. The only difference is that we would use the array notation to do so. In this next code, we add the first and second elements together and store the answer in the third.

aBunchOfInts [3] = aBunchOfInts [0] + aBunchOfInts [1];

Arrays can also interact seamlessly with regular variables, like this perhaps.

int a = 9999;

aBunchOfInts[4] = a;

Initializing the elements of an array

[widgets_on_pages id=”udemy_advert_cpp_2″][widgets_on_pages id=”udemy_code_details”]
We can more quickly add values to the elements like this example that uses a float array.

float myFloatingPointArray [3] {3.14, 1.63, 99.0};

Now the values 3.14, 1.63 and 99.0 are stored in positions 1, 2 and 3 respectively. Remember that when using array notation to access these values we would use 0,1 and 2.

There are other ways to initialize the elements of an array. This slightly abstract example shows using a for loop to put the values 0 through 9 into the uselessArray array.

for(int i = 0; i < 10; i++){

     uselessArray[i] = i;


So what do these arrays really do for me?

The real benefit of arrays in our game code was hinted at the start of the tutorial. Arrays can hold objects (instances of classes).

Imagine that we have a ZombieEnemy class and we want to store a whole bunch of them. We could do so like this:

ZombieEnemy horde [5] {zombieEnemy1, zombieEnemy2, zombieEnemy3... etc};

The horde array now holds a load of instances of the ZombieEnemy class. Each one is a separate, living(kind of), breathing, self-determining Zombie Enemy. We could then loop through the horde array each pass through the game loop moving the zombies, checking if their heads have met with an ax or if they have managed to catch the player.

The best way to get a feel for this is to see it in action.