Arrays

From Chilipedia
Jump to: navigation, search

Arrays are a collection of elements usually of the same type grouped together and referenced using an index to a unique identifier.

If you think of variables as boxes in which you can keep stuff and label them box0, box1 ... arrays can be thought of as a large box with compartments in it, where the box can be called box and each compartment is labeled box[0], box[1], box[2] and so on.


Using Arrays

Syntax

// create a big box that stores integers with 10 compartments
int box[10];

// Now to put values in these compartments all we have to do is
box[0] = 50;
box[1] = 89;
box[2] = 69;
box[3] = 50;
.
.
.
box[9] = 30;

// NOTE - There are 10 comparments in the box, so we start with compartment 0 
//        and go all the way to compartment 9.

Notice how the array declaration follows the format:

// TypeOfBox NameOfBox[CompartmentsInBox]
	int       box         [10]

// To get access to a compartment use use the syntax  
// NameOfBox[CompartmentNumber]
	box[0] // Gives you access to the first compartment in the box. 
	box[1] // Gives access to the second compartment
   box[9] // Gives access to the last compartment

Other ways to declare an array:

// An array of 20 ints
int box[20];

// An array of 5 ints where the first three values are initialised, the other two have default values
int box[5] = {50, -35, 69};

// Create an array of ints and initialize all elements to zero
int box[10] = {}

// Both of these statements define an array of 3 ints initializes them with the values provided. 
int box[] = {50, 23, 40};
int box[]{50, 23, 40};

The size of the array should be known at compile time and the size cannot be changed throughout the lifetime of the program. This is because the space for your container needs to be known at compile time

// FAIL 
int size = 10;
int box[size]; // Error: Here the variable value can be changed during the life of the program because it is not a constant

// PASS
constexp int size = 10; //  When using constexr it is guaranteed that size won't be changed and it will be known at complie time.
int box[size];

Accessing compartments that don't exist is a common mistake, in debug mode you'll get an error but bad things will happen in release mode. Especially be careful while accessing the last compartment of the array.

box[20] = 69 // Gives an error as our box only has 10 compartments and we're trying to access the 21st compartment. 
box[10] = 69 // Again gives an error as our box has 10 compartments starting at 0, and going upto 9

But why arrays ?

Imagine you wanted to store the health of 10 enemies you could store them as enemyHealth0, enemyHealth1 and so on

// Store the health of 10 enemies 
int enemyHealth0 = 44
int enemyHealth1 = 35
int enemyHealth2 = 45
.
.
.
int enemyHealth9 = 84

So far so good, now lets say you wanted to display the health of all 10 of your enemies

DisplayHealth(enemyHealth0);
DisplayHealth(enemyHealth1);
DisplayHealth(enemyHealth2);
.
.
.
DisplayHealth(enemyHealth9);

As you can probably see that you need to type DisplayHealth() repeatedly , what if we could do the same thing but with a lot less code ? That's where arrays come in:

// Declare an array of size 10 and set health variables to
int enemyHealth[10] = {44, 35, 45 ... 84};

// use a for loop to diplay enemy health
for(int i = 0; i < 10; i++)
{
	DisplayHealth(enemyHealth[i]);
}

We could do the same if we wanted to kill off all enemies, just go through an array of all the enemies and kill them. Arrays work really well with loops, in the above example we only had 10 values, but imagine if we had 100 values we could just loop over all of them with the same code by just changing the condition to i < 100 .

What's really going on here tho

When you create an array e.g. int box[10] your computer takes a portion of your memory and gives a name to that block i.e. box in our case. The size of the block is decided by the type of the array. In our case int, a single int is 4 bytes in size MSDN Reference and we need space to store 10 ints so your computer allocates 10 x 4 bytes of memory i.e. 40 bytes of memory where each int occupies 4 bytes one after the other.


Now when you access the third compartment in your array box and use the syntax box[3]. Your CPU says I am going to go to the starting point of box , you want the 3rd compartment? Ok, so each compartment is 4bytes long (int) so to get to the third compartment I can take the starting point of block and move 3 times the size of each compartment, which in our case would be 3 * 4 = 12 bytes . Now we are at the starting point of the third block. And because the type of the array is an int (size 4 bytes), your CPU knows that the next 4 bytes in the memory block are the contents of compartment 3.


If you're starting out with arrays, you don't need to know how arrays work under the hood; more gems will be revealed as you move on to the intermediate series. What's important is that you get a solid understanding of the concept of arrays as being big compartmentalized containers to store stuff that work really well with loops.

Planet Chili Tutorial Video

Other Tutorials

See Also