Arrays are one of the most powerful and widely used data structures in programming. In this tutorial, we’ll learn about C++ arrays and how to create an array in C++.

What are Arrays?

An array is a fixed-sized data structure containing more than one data value of the same type. It is composed of small contiguous blocks in memory which store these data values. An array has a fixed size. It means while declaring an array, we must specify the size/length of it. Once the size is defined, we cannot change it later. 

In the above figure, each block is a memory segment containing values 10, 20, 30, …, 100. Note that these blocks are contiguous and hence they combine together to make an array of size 10 to store integer values. At any particular point, an array can store the values of the same data type. For instance, we can store all string values in an array and all integers value in another array but we cannot store a combination of strings and integers in a single array. 

Another way to look at arrays is to imagine it as a large chunk of memory divided into smaller blocks of memory and each memory block is capable of storing data value of some type.

I hope it is clear from the above definition and explanation that what actually arrays are! Let’s now dive deep into discussing their implementation in C++ programming. 

C++ Arrays

We can create a C++ array in one of the two ways; stack-based and heap-based. If you wish to learn more about stack-based and heap-based memory allocation techniques, you can explore them in great detail here.

Let’s discuss each of them step by step.

Stack-based Array

Using the following syntax, we can create an array using stack-based memory allocation in C++.

<data_type> <variable_name>[<array_length>];

The above syntax is how you can create an array using stack-based memory allocation. It declares an array of type data_type with name variable_name and length array_length.

Let’s look at an example to properly understand it:

int myArray[5];

The above code piece declares an array of integers of length 5 with the name myArray.

This is just a simple declaration of an array that doesn’t define the values inside the array. Let’s look at how we can define these values. 

We can define the values of an array in the following two ways:

Method #1:

int myArray[5] = {1, 2, 3, 4, 5};

Method #2:

int myArray[5];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;

To access a value within the array, you have to know its index. Here’s an example:

cout << myArray[3];
// Output: 4

The above example prints the fourth value stored in the array. Remember that the index in C++ arrays (and most other programming languages) begins from 0, hence the reason why we used index 3 to access the fourth value in the array.

Heap-based Array

Using the following syntax, we can create an array using heap-based memory allocation in C++:

<data_type>* <variable_name> = new <data_type>[<array_length>];

We can declare the same array using heap-based memory allocation as well. This time, the variable_name is a pointer variable (*) of type data_type and length array_length. Let’s look at an example to get a real sense of the above syntax.

int* anotherArray = new int[5];

The above code piece does the same thing. It declares an array of integers of size 5 by the name anotherArray. This time it uses heap-based memory allocation and pointer variable (*) to declare this array.

To populate the values inside this array, you can do the following:

int anotherArray[5];
anotherArray[0] = 1;
anotherArray[1] = 2;
anotherArray[2] = 3;
anotherArray[3] = 4;
anotherArray[4] = 5;

That’s all about C++ arrays. Think of some interesting use cases where you can practically use arrays while programming in C++. If you’re aware of Object Oriented Programming, then you can build countless significant use cases with the implementation of arrays.

If you’re new to C++ programming, you might want to take a look at our C++ tutorials on conditional statements, for loops and while loops. For more tutorials, check out our collection of C++ guides.