Regardless of your C++ experience, as a programmer you’ve likely come across the concept of an array. Put simply, an array is a collection of data (i.e., a data structure) that allows you to store groups of objects by type. An array holds these values next to each other in memory.

For example, say you want to survey 500 people and you’d like to store their ages. In most programming languages, you could do this by creating an array of length 500. Now, let’s see how this might look in C++. 

What Is an Array in C++?

In some languages, an array does not depend on the type of data it holds; arrays can even store mixed data types. But when you start working with C++, you’ll find that arrays are of a fixed type. In other words, arrays in C++ are specified at declaration. The usual way of declaring an array is to provide the type name, variable name and size (in brackets):

To continue with our survey example, you can create an array for these 500 individuals’ ages by declaring an integer array with 500 elements:

It’s also possible to initialize the array, by which you would specify the values in the array at the time of declaration. For example, let’s say we now only want to interview five people. We could initialize our array upon declaration with the following code:

You can also initialize an array without specifying the number of elements:

Although the compiler can determine the number of elements you put inside braces and makes that count the array size, it’s best practice to specify array size. Why? In this example, if you were to think the size of the above array is six (it’s really five) and treat it as such, your code would throw an error upon your next access. But you explicitly declare it as an array of size 5, you’ll have an easy reference to it even if you don’t remember its size later on. 

Specifying the array size also allows the compiler to issue an error if the number in brackets does not match the given size upon declaration. This is a safeguard for future use of that array — it’s better to pinpoint your error at its source than to have to debug when your code does not compile.

Before diving further into C++ arrays and how to use them, let’s talk about what they are not.

Differences Between a C++ Array and a Vector

Those who have dabbled in C++ may be familiar with the vector data structure and have perhaps noticed several similarities between vectors and arrays. What exactly distinguishes them from each other?

Most importantly, a vector is dynamically sized, which means that additional memory is allocated as needed. The size of an array is fixed (i.e., it can’t be changed) and is thus more memory-efficient. Whereas you would usually specify the size of an array at the declaration, with a vector you never need to specify size — as it’s constantly changing.

Still thinking that vectors and arrays look similar? Well, you’re right: A vector is more or less a one-dimensional array. But once we examine some use cases for each, it’ll be clear as to why programmers distinguish between the two. 

Because arrays are index-based, they are ideal for scenarios where we want to frequently access their elements. For example, take the fifth person we interviewed in our survey of 500: How old were they again? By contrast, vectors are best suited for frequent insertion and deletion, or for simply storing information without needing to know the number (or any other data type) at a certain location. For additional differences between vectors and arrays, check out this helpful summary.

Use Cases for Arrays in C++

Now that we have an idea of what C++ arrays are, we can guess that their most common use case is storing and grouping variables of the same type for later access. Below, we’ll talk about how to use arrays like tables with columns and rows for storage, but for now, let’s dive into array functionality.

From our survey where we had to record the ages of 500 individuals, we learned how to declare and initialize arrays. But how do we access and manipulate them like a pro?

It’s simple to read from and write to an array. For example, say we made a mistake in recording the third survey subject’s age as 16, when she’s really 26. Oops! We can overwrite the third element (remember, it’ll be index 2) like so:

In C++, cin is used to insert data and cout is used to print it. So we have the following:

Let’s tie all this together and see what a for-loop using a C++ array might look like:

First, we declare all the variables that we need. The size() function is a convenient way to get the size of the Ages array for when we iterate over it.

Now let’s look at the for-loop itself. We want to iterate through our Ages array by accessing the value in each index and adding it to the totalAge variable we defined above. We can do this by starting our iterator at 0, which will be used as the first array index, and incrementing it until we hit the last index, (numSurveyees – 1), in our Ages array.

As you can see in the last line of the above code, we used the final result of our for-loop (the totalAge sum we achieved in our last iteration) to calculate the average age of those we surveyed.

Fun Facts About C++ Arrays

Let’s look at some interesting facts about the functionality of C++ arrays. One of the reasons programmers might choose to use arrays over vectors is that arrays can be two-dimensional, or three-dimensional … or even ten-dimensional. Vectors, by contrast, are always one-dimensional.

What this means for arrays is that you can have an array within an array, also known as a nested array. Technically there’s no limit on how many levels deep they go. Is this simply meant to be confusing? Not at all, in fact: Although deeply nested arrays are uncommon in C++, two-dimensional arrays are frequently used to store data in table-like structures, as we will see below.

Values in two-dimensional arrays are usually referenced by the notation x[i][j], with i being the row number and j being the column number. We can declare a two-dimensional integer array “myTable” as follows:

Initializing a two-dimensional array calls for the use of nested braces. The above example array contains two rows, so we must use two sets of inner braces to initialize it. We might initialize this 2D array with the following values:

Imagine putting the second inner array underneath the first, and you will see how they make up a table with two rows and four columns.

Although we will stop short of initializing it, a three-dimensional array would be declared in much the same way:

Here, you can imagine a rectangular prism with a width of 5, a length of 6 and a height of 7. We’ll leave off here before things become a little too multi-dimensional, but you can imagine how complex things can get with the ability to nest arrays endlessly.

Learn C++ Online

We hope you learned something new from our breakdown of C++ arrays. If you’re comfortable with C++ and want to become a full-fledged C++ developer, check out our expert-taught C++ Nanodegree program, where you’ll learn the language by coding five real-world projects.

Start Learning