[Total: 2   Average: 4.5/5]

Arrays are one of the foundations of data in almost every programming language. The idea of what an array is in C++ is not that much different from arrays in other languages such as Swift, Java, and JavaScript.

But what is an array? How does it work? And what does it look like in a C++ context?

Image result for c++

What is an array?

Imagine you had a piece of data. Under some circumstances, this data is a single piece of information. It can be assigned to a primitive type like a string if it is a set of letters, or an int if it is a number.

For example:

string car = "Tesla";
int phone = 8374827888;

However, some pieces of data can be complex. What if you wanted to hold more than just one piece of information against a single variable? What if you wanted a list?

This is essentially what an array is – a list. An array is a data type that can hold multiple values against a single variable. An array is identifiable through a pair of [] and looks something like this:

string cats[4] = {"tuxedo", "ginger", "black", "striped"};

The number inside [] signifies how many items there are in the list. The string declares the list as a list of string typed values.

But what if you don’t know the size of your array? Can you create an array without specifying how many items there are in the list? The answer is yes.

You don’t have to specify the array size and can write it with a pair of empty []. The array will only be as big as the number of items assigned.

For example:

string cats[] = {"tuxedo", "ginger", "black"};
//the size of the array is 3

On the surface, this just seems so much easier. However, in C++, if you specify the array size, it won’t be able to accommodate future values. In the above example, there is only enough space for three items. You can’t add any more to it easily without overwriting the original values.

When you specify the array size, you are preserving memory space for it. This means that you insert more items into the arrays as needed without being required to overwrite the entire array.

For example:

//when writing arrays with an unspecified number
string cats[] = {"tuxedo", "ginger", "black"};
//doing this won't work
cats[3] = "striped";
//but this will
cats[] = {"tuxedo", "ginger", "black", "striped"};

//however, when space is reserved, this will work
string cats[5] = {"tuxedo", "ginger", "black"};
cats[3] = "striped";

When you declare an array with the size attached, it also allows you to create an array without any values attached. This can come in useful if you want to add values later.

For example:

string cats[4];
cats[0] = "tuxedo";
cats[1] = "ginger";

It is good to note that in C++, you can’t create mixed type arrays. So, a piece of information that looks something like this cannot be assigned to an array.

{"tuxedo", 1, "banana", 1.34}

An array in C++ can be assigned against any primitive types available.

How to access arrays in C++

To access a specific value inside an array, you need to match its position in the list by signifying it inside the [].

For example:

string cats[4] = {"tuxedo", "ginger", "black", "striped"}
count << cats[0];
//outputs "tuxedo"

To change a value inside an array, you can just do it via the = assignment operator and matching it with the position inside the [].

For example:

cats[1] = "Ginger";
//ginger will now be Ginger

Looping through an array can be done through a for loop and looks something like this:

string cats[4] = {"tuxedo", "ginger", "black", "striped"}
for(int i = 0; i < 4; i++){
  cout << cats[i] << "\n";

The i is representative of the index value and for every cats[i], the associated value will be printed.

Diving deeper into Arrays in C++

So that’s the syntax basics of arrays. But surely, there’s more to it than that. To quench your curiosity – you are very much correct.

When it comes to arrays in C++, it is a collection of items stored inside contiguous memory locations. This is why the array size needs to be declared, especially if you’ve got plans for it in the future.

Every time an array is created, memory is reserved for it. C++ is a highly efficient and strict language, meaning that it will only reserve as much memory as it needs and does not automatically scale.

The major advantage of arrays in C++ is that it allows random access of elements via the array’s index. It uses less code and can assign multiple elements at once. Running through an array is as simple as using a loop.

However, the drawback of arrays in C++ is that it works on a fixed number of elements. This means that once it’s created, you cannot add any more. This is because an array in C-based languages is not dynamically typed like it is in JavaScript.

What about Multidimensional Arrays in C++?

A multidimensional array is when there are more than one value inside a value. This sounds a little confusing, so let’s look at an example.

int x[2][4];

The above is declaring a data matrix that looks something like this:

        col 1   col 2   col 3   col 4
row 1 x[0][0] x[0][1] x[0][2] x[0][3]row 2 x[1][0] x[1][1] x[1][2] x[1][3]

The actual data can look something like this:

int x[2][4] = {
  {1, 2, 3, 4},
  {5, 6, 7, 8}

As you can see, there are two rows of data with four columns inside each row.

For example, if you had something like this:

int x[2][3][4];

Your data would look something like this:

int x[2][3][4] = {
  {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}},
  {{13, 14, 15, 16},{17, 18, 19, 20},{21,22,23,24}}

To decrypt the above declaration, x[2][3][4] can be read as the following:

  • there are 2 rows of data
  • each row has 3 sets of data
  • each set of data has 4 values inside

It also means that the maximum value that x[2][3][4] can hold is 24 values. To loop through the array to get to a specific value, you’ll need to create loops with matching depths.

Final thoughts

That’s basically arrays in a nutshell for C++.

If you’re new to the concept, just think of it as a list, and to access a specific value, you only need the associated coordinates. The maximum number of values you can store inside an array is based on the multiplication of all its dimensions.

For example, x[3] will have a maximum of 3 values attached to it. x[3][3] will have a maximum of 9 values inside its array. x[3][4][5] will have 60 spaces available and provisioned storage associated with it.

About the author

I code. I write. I hustle. Living the #devLife remotely. Full-stack MVP developer. Coding and writing about code.