0x04 - Arrays
Arrays in C++
An array is a contiguous set of elements stored in memory. Imagine you wanted to store 10 numbers ranging from 1 to 10. You could do it like this:
int number0 = 1;
int number1 = 2;
int number2 = 3;
int number3 = 4;
int number4 = 5;
int number5 = 6;
int number6 = 7;
int number7 = 8;
int number8 = 9;
int number9 = 10;
You successfully stored 10 numbers in memory, but this isn’t very flexible. You’ve created 10 variables and there is nothing tying them together apart from the similar name. For example, there is no easy way to pass all these variables to a function as an argument. So, C++ has a concept of arrays, which allows you to write this instead:
int myArray[10];
int myArray[10];
looks very similar to a normal variable declaration, right? That’s because it is. But what is the type of the variable? You see there is int
specified in front of the variable name, but there is also a number 10 in square brackets after the variable name.
The square brackets say that we’re creating an array of the type we’ve specified (int
in this case). The number in the brackets denotes the length of the array or, in other words, how many elements of our type we can fit into the array. So here, we have created a variable called myArray of type int[10]
, which you can read as an array of 10 integers.
Compared to the previous example, we now have only one variable that we can work with, but that doesn’t stop us from accessing every element of the array individually. To access the elements, we can use the same []
operator like this: myArray[2]
, which accesses the 3rd element. It’s accessing the 3rd element and not the 2nd because in C++, indexing starts with 0, so myArray[0]
would access the first element of the array and myArray[9]
would access 10th (last) element of our array.
So, we have an array but we haven’t initialized it yet. We have several ways to initialize the array. We can assign values to the array elements like this:
int myArray[10];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;
myArray[5] = 6;
myArray[6] = 7;
myArray[7] = 8;
myArray[8] = 9;
myArray[9] = 10;
But not only this is slow to write and barely readable, it’s also fairly error-prone. There is a better way to initialize our array with numbers from 1 to 10:
char myArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
This is called an aggregate initialization which allows us to declare and initialize the array in one step. It’s the same principle that we’ve already covered in chapter 0x03 when we first created a variable and then assigned a value to it, versus when we declared and initialized it in one go:
int a;
a = 10;
versus:
int a = 10;
The compiler has one trick up its sleeve when it comes to array initialization - it can calculate the length of the array for us so we don’t even need to put the array length in the brackets when initializing the array using aggregate initialization:
char myArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
This will also create an array of 10 integers because in the initialization list, we have specified 10 elements.
In the next chapter, I will show you how arrays can be very useful when dealing with text.