Last Updated : 27 Jul, 2025
An array is a collection of elements of the same type placed in contiguous memory locations. It allows you to store multiple values under a single name and access them using an index.
Arrays are one of the most basic and commonly used data structures in C++ programming
#include <iostream>
using namespace std;
int main() {
// declaring and initializing an array of size 5
int arr[5] = {2, 4, 8, 12, 16};
// printing array elements
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
Explanation:
We can create/declare an array by simply specifying the data type first and then the name of the array with its size inside [] square brackets(better known as array subscript operator).
C++
data_type array_name [size]
This statement will create an array with name array_name that can store size elements of given data_type. Once the array is declared, its size cannot be changed.
Example:
C++
This will create an array with name arr that can store 5 integers.
Array DeclarationWhen we declared an array, the elements of array do not contain any valid value.
Initialize the ArrayInitialization means assigning initial values to array elements. We can initialize the array with values enclosed in curly braces '{}' are assigned to the array. For example:
C++
int arr[5] = {2, 4, 8, 12, 16};
These values will be assigned sequentially. It means that the first element (index 0) will be 10, second will be 20, and so on. The number of values in the list cannot be more than the size of the array. But they can be less that the size. This is called partial initialization.
C++
The size of the array can be skipped if the size should be same as the number of values.
C++
int arr[] = {2, 4, 8, 12, 16};
Array Initialization
Moreover, all the elements can be easily initialized to 0 as shown below:
C++
This method only works for 0, but not for any other value.
Note: The value assigned should be of the same type of the array elements specified in the declaration
Access Array ElementsElements of an array can be accessed by their position (called index) in the sequence. In C++, indexes of an array starts from 0 instead of 1. We just have to pass this index inside the [] square brackets with the array name as shown:
C++
It is important to note that index cannot be negative or greater than size of the array minus 1. (0 ≤ index ≤ size - 1). Also, it can also be any expression that results in valid index value.
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {2, 4, 8, 12, 16};
// Accessing fourth element
cout << arr[3] << " ";
// Accessing first element
cout << arr[0];
return 0;
}
Update Array Elements
To change the element at a particular index in an array, just use the = assignment operator with new value as right hand expression while accessing the array element.
C++
array_name[index] = value;
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {2, 4, 8, 12, 16};
// Updating first element
arr[0] = 90;
cout << arr[0] << endl;
return 0;
}
Updating Array Element Traverse Array
Traversing means visiting each element one by one. The advantage of array is that it can be easily traversed by using a loop with loop variable that runs from 0 to size - 1. We use this loop variable as index of the array and access each element one by one sequentially.
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Traversing and printing arr
for (int i = 0; i < 5; i++)
cout << arr[i] << " ";
return 0;
}
There are more methods to traverse array that are listed here - Traverse an Array in C++
Size of ArrayThe size of the array refers to the number of elements that can be stored in the array. The array does not contain the information about its size but we can extract the size using sizeof() operator.
C++
#include <iostream>
using namespace std;
int main() {
char arr[] = {'a', 'b', 'c', 'd', 'f'};
// Size of one element of an array
cout << "Size of arr[0]: " << sizeof(arr[0])
<< endl;
// Size of 'arr'
cout << "Size of arr: " << sizeof(arr) << endl;
// Length of an array
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Length of an array: " << n << endl;
return 0;
}
Size of arr[0]: 1 Size of arr: 5 Length of an array: 5
To know more methods, refer to the article - Length of Array in C++
Arrays and PointersIn C++, arrays and pointers are closely related to each other. The array name can be treated as a constant pointer that stored the memory address of the first element of the array.
C++
#include <iostream>
using namespace std;
int main() {
int arr[5];
// Printing array name
cout << arr << endl;
// Printing address of first element
cout << &arr[0];
return 0;
}
0x7ffd57920530 0x7ffd57920530
Internally, arrays operators are performed using pointer arithmetic. So, we can do almost any array operation using by using pointer to the first element. For example, we can access all the elements of an array using pointer to the first element.
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {2, 4, 8, 12, 16};
// Define a pointer to first element
int* ptr = arr;
for (int i = 0; i < 5; i++)
cout << *(ptr + i) << " ";
return 0;
}
Refer to this article to know more - Relationship Between Pointer and Array
Pass Array to FunctionArrays are passed to functions using pointers, as the array name decays to a pointer to the first element. So, we also need to pass the size of the array to the function.
C++
#include <iostream>
using namespace std;
// Function that takes array as argument
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main() {
int arr[] = {2, 4, 8, 12, 16};
int n = sizeof(arr) / sizeof(arr[0]);
// Passing array
printArray(arr, n);
return 0;
}
There are also other ways to pass array to functions. Refer to the article - Pass Array to Functions
Multidimensional ArraysIn the above examples, we saw 1D (one dimensional) array. This array's size can only increase in a single direction (called dimension). C provides the feature to have as many dimensions as desired for an array. Arrays declared with more than one dimension are called multidimensional arrays.
Create Multidimensional Array C++
data_type array_name [size1][size2]...
where size1, size2 ... are the sizes of each dimension.
The complexity of the array operations increases exponentially with increase in dimensions.
Some commonly used multidimensional arrays are:
The below practice problems provide you some commonly used scenarios and exercises for practicing arrays in C++:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4