Last Updated: January 3, 2026
Imagine having a collection of variables, all of the same type, neatly organized under a single name.
This is essentially what an array does.
It allows you to store multiple values efficiently, and once you get comfortable with arrays, you’ll find them incredibly useful in a wide range of programming tasks.
At its core, an array is a data structure that holds a fixed number of elements, all of the same type. You can think of it as a row of boxes, where each box can hold a value. When you define an array, you specify its size, which determines how many elements it can hold.
For example, let’s say you want to store the scores of five students. Instead of creating five separate variables, you can use an array:
Here, scores is an array that can hold five integers. Each element can be accessed using an index, which starts at zero. So, scores[0] will give you the first student's score, scores[1] the second, and so on.
Remember, in C++, arrays are zero-indexed, meaning the first element is accessed with index 0.
Declaring an array is just the first step. You’ll often need to initialize it with values. There are several ways to do this in C++:
You can initialize an array at the time of declaration:
In this case, the array scores is created with the specified values. If you don’t provide enough values for all elements, the remaining elements will be initialized to zero.
You can also partially initialize an array:
Here, scores[2], scores[3], and scores[4] will automatically be set to zero.
C++ allows you to omit the size during initialization. The compiler will determine the size based on the number of initial values:
This is handy, as it simplifies declaration when you already know the values you want to use.
Sometimes, you might not know the values at compile time. You can initialize an array during runtime:
This approach is useful when collecting data from users or processing input dynamically.
Accessing elements in an array is straightforward. You use the index to retrieve or modify the value:
Often, you will want to process each element in an array. A common way to do this is with a loop. Here’s how you can print all the scores:
Using a loop like this makes your code concise and avoids repetitive code.
When iterating through arrays, always be mindful of the bounds to avoid accessing out-of-bounds elements, which can cause undefined behavior.
Now that we’ve covered the basics, let’s dive into some common operations you might perform with arrays.
A common task is to find the maximum value in an array. Here’s a simple way to do that:
Calculating the sum of all elements is another frequent operation:
Reversing an array is a common algorithmic challenge. Here’s how you can do it:
While arrays are powerful, they come with some common pitfalls that can trip up even seasoned developers. Let's discuss a few of these.
One of the biggest dangers with arrays is accessing an index that doesn’t exist. If you try to access or modify an element outside the bounds of the array, you will invoke undefined behavior:
It’s always a good practice to check that your index is within valid bounds.
Using uninitialized elements can also lead to unpredictable results. If you declare an array without initializing it, the elements will contain garbage values:
Always initialize your arrays, either statically or dynamically, to ensure they contain valid data.
Arrays in C++ have a fixed size, which means once you declare an array, you cannot change its size. If you find that you need a resizable structure, consider using a std::vector from the Standard Template Library (STL), which provides dynamic sizing.
Now that you understand the basics of arrays, you'll be ready to explore multidimensional arrays.
In the next chapter, we will dive into how to use arrays in multiple dimensions, unlocking new ways to represent and manipulate data efficiently.