C++ Multidimensional Array
Last Updated :
16 May, 2025
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.
Syntax of Multidimensional Arrays
C++
data_type array_name[s1][s2]...[sn];
where s1, s2,…, sn are the size of each dimension.
Example:
C++
// Two dimensional array
int two_d[2][4];
// Three dimensional array
int three_d[2][4][8];
Multidimensional Arrays Size
For 1D array, the length of the array is simply its size too. But multidimensional arrays have extra dimensions. So, the size of each dimension is considered separately. The size of multidimensional array is the product of all its dimensions' size. It is similar to calculating area in 2D and volume in 3D.
For example, consider the below array:
C++
- The array int arr[2][4] can store total (2 * 4) = 8 elements (product of its dimensions).
- The size in bytes can be calculated by multiplying the number of elements by size of each element or we can just use sizeof operator.
- In this case, size in bytes = 4*8 = 32 bytes.
To verify the above calculation, we can use sizeof() method to find the size of an array.
C++
#include <iostream>
using namespace std;
int main() {
// creating 2d and 3d array
int arr[2][4];
// using sizeof() operator to
// get size of arr
cout << sizeof(arr) << " bytes";
return 0;
}
We can have any number of dimensions in an array as per requirement, but the complexity of handling them also increases exponentially. That is why, the most widely used multidimensional arrays are:
- Two-Dimensional Array (2D Array)
- Three-Dimensional Array (3D Array)
2D Array
A two-dimensional array in C++ is a collection of elements organized the form of rows and columns. It can be visualized as a table or a grid.
2D ArrayCreate 2D array
C++
Initialize 2D Array
Like 1D arrays, 2D arrays can also be initialized using a list of values enclosed inside {} curly brackets, but as 2D arrays have two dimensions, the list is nested inside another list to initialize each dimension one by one. It means that each row values are nested inside one big list.
C++
int arr[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}};
Nesting can also be omitted, and values will still be assigned sequentially.
C++
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
The above array has 2 rows and 4 columns. The elements are filled in a way that the first 4 elements are filled in the first row and the next 4 elements are filled in the second row. The values will be initialized sequentially.
It is to be noted that the number of values should not exceed the total number of elements an array can store. It can have less values (partial initialization) but cannot have more values.
If all the elements are to be initialized to 0, then this syntax can be used:
C++
This can be only done for 0, not for any other value.
Access and Update Elements
Elements of a 2D array have to be accessed using row and column indices inside [] square brackets. It is similar to matrix element position, but the only difference is that here indexing starts from 0.
The value of any element can be updated by using = assignment operator.
C++
arr[i][j];
array_name[i][j] = new_value
where, i is the index of row, j is the index of the column, and new_value to updated. The range of indexes should be:
- 0 ≤ i ≤ (row_size - 1)
- 0 ≤ j ≤ (col_size - 1)
Any values other than that leads to the segmentation fault.
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
// Accessing 3rd element is 1st row
cout << arr[0][2] << endl;
// Accessing first element in 2nd row
cout << arr[1][0] << endl;
// Updating 3rd element is 1st row
arr[0][2] = 22;
cout << arr[0][2] << endl;
// Updating first element in 2nd row
arr[1][0] = 99;
cout << arr[1][0];
return 0;
}
Traverse 2D Array
Two loops nested inside each other are needed to traverse a 2D array. First loop is used to move though the rows of 2D array, while other is used to move though columns in each row to access all the elements of the row.
C++
#include <iostream>
using namespace std;
int main() {
int arr[2][4] = {0, 1, 2, 3, 4, 5, 6, 7};
// Outer loop to move through rows
for (int i = 0; i < 2; i++) {
// Inner loop to move though elements in each row
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
3D Array
A three-dimensional array in C++ is a collection of elements organized in a 3D cuboid-like structure. It can be visualized as a series of two-dimensional arrays stacked on top of each other.

Create 3D Array
To declare a 3D array in C++, we need to specify its third dimension along with 2D dimensions.
C++
Initialize 3D Array
Like 2D arrays, 3D arrays can also be initialized using a list of values enclosed inside {} curly brackets. However, in a 3D array, the values are grouped into 2D arrays, and each 2D array is nested inside another set of curly brackets.
C++
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
Alternatively, nesting can be omitted, and the values will still be filled sequentially:
C++
int arr[2][2][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
This array has 2 layers (depth), 2 rows per layer, and 3 columns per row. The values are filled sequentially across the layers.
If all the elements are to be initialized to 0, then this syntax can be used:
C++
This can be only done for 0, not for any other value.
Access and Update Elements
The elements of a 3D array are accessed and updated using three indices: depth, row, and column. These indices must be within the following ranges:
- 0 ≤ depth ≤ (depth_size - 1)
- 0 ≤ row ≤ (row_size - 1)
- 0 ≤ column ≤ (column_size - 1)
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
// Accessing element at depth
// 0, row 1, column 2
cout << arr[0][1][2] << endl;
// Accessing element at depth
// 1, row 0, column 1
cout << arr[1][0][1] << endl;
// Updating element at depth
// 0, row 1, column 2
arr[0][1][2] = 22;
cout << arr[0][1][2] << endl;
// Updating element at depth \
// 1, row 0, column 1
arr[1][0][1] = 99;
cout << arr[1][0][1];
return 0;
}
Traverse 3D Array
To traverse a 3D array, you need three nested loops: one for each dimension (depth, row, column).
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[2][2][3] = {
{{0, 1, 2}, {3, 4, 5}},
{{6, 7, 8}, {9, 10, 11}}
};
// Traverse through all elements of 3D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
}
return 0;
}
Output0 1 2
3 4 5
6 7 8
9 10 11
Pass Multidimensional Arrays to Functions
In C++, you can pass multidimensional arrays to functions. Since multidimensional arrays have more than one dimension, the function signature needs to account for all dimensions.
Passing a 2D Array to a Function
To pass a 2D array to a function, you can specify the number of columns (or other dimensions) in the function signature. The number of rows can be deduced automatically.
C++
#include <iostream>
using namespace std;
// Function to print a 2D array
void print2DArray(int arr[2][3]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
// Initializing a 2D array
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Passing the 2D array to the function
print2DArray(arr);
return 0;
}
Passing a 3D Array to a Function
To pass a 3D array to a function, you need to specify the size of the second and third dimensions.
C++
#include <iostream>
using namespace std;
// Function to print a 3D array
void print3DArray(int arr[2][3][4]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
}
}
int main() {
// Initializing a 3D array
int arr[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}}
};
// Passing the 3D array to the function
print3DArray(arr);
return 0;
}
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Top 50 Array Coding Problems for Interviews Array is one of the most widely used data structure and is frequently asked in coding interviews to the problem solving skills. The following list of 50 array coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.Easy ProblemsSecond Largest
2 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Maximum Subarray Sum - Kadane's Algorithm Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -4}Output: -2Explanation: The subarray {-2} has the largest s
9 min read