22
DecMultidimensional Arrays in C: 2D and 3D Arrays
Multidimensional Arrays in C: An Overview
We already saw what is Array in C. Now you might have become familiar with the basic concepts ofarray
in C. Let us now begin with the Multidimensional Arrays in C in our C tutorial. To certify your skills, you can also look into our C Certification Course.Multidimensional Arrays in C
Arrays
can have any number of dimensions. In C programming, you can create an array of arrays.Syntax
To create a multidimensional array in C, you need to specify the number of dimensions and the size of each dimension.dataType arrayName[size1][size2]...[sizeN];
Example
int arr[5][4];
Here, an integer type 2D array, “arr
” is declared with 5
rows and 4
columns thus a total of 20
integer elements.
In the same manner, as above, a 3D array can be declared.
Example
int arr[2][4][3];
The array arr
can store 24
integer elements.
How to initialize a multidimensional array in C?
2D array
It is a matrix (a table of rows and columns).int arr[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[2][3] = {1, 3, 0, -1, 5, 9};
3D array
The initialization is similar to a 2D array.int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};
Read More - Top 50 Mostly Asked C Interview Questions
How to access the elements of a multi-dimensional array in C?
2D array
In this type of array, you must specify theindex
number of both the row and column.Example
#include <stdio.h>
int main() {
int arr[2][3] = { {5, 6, 7}, {3, 6, 8} };
printf("%d", arr[0][2]);
return 0;
}
The printf
statement in the above code accesses the value of the element in the first row (0
) and third column (2
) of the array, “arr
”.
Output
7
3D array
Example
#include <stdio.h>
int main() {
int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};
printf("%d", arr[0][2][3]);
return 0;
}
The printf statement in the above code in the C Online Compiler accesses the value of the element in the first row (0
), the third column (2
), and fourth dimension (3
) of the array, “arr
”.
Output
2
How to change the elements of a multidimensional array in C?
To change the value of a specific element, refer to the index
number in each dimension in the C Editor.
2D array
Example
#include <stdio.h>
int main() {
int matrix[2][3] = { {5, 6, 7}, {3, 6, 8} };
matrix[0][2] = 9;
printf("%d", matrix[0][2]); // Now outputs 9 instead of 1
return 0;
}
Output
9
3D array
Example
#include <stdio.h>
int main() {
// Define a 3D array
int array3D[2][3][4];
// Initialize the 3D array
int value = 1;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
array3D[i][j][k] = value++;
}
}
}
// Display the original 3D array
printf("Original 3D Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("%3d ", array3D[i][j][k]);
}
printf("\n");
}
printf("\n");
}
// Change elements in the 3D array
array3D[0][1][2] = 100;
array3D[1][0][3] = 200;
// Display the modified 3D array
printf("\nModified 3D Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("%3d ", array3D[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output
Original 3D Array:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
Modified 3D Array:
1 2 3 4
5 6 100 8
9 10 11 12
13 14 15 200
17 18 19 20
21 22 23 24
How to traverse a multidimensional array in C?
To loop through a multi-dimensional array, you need one for
loop for each of the array's dimensions.
2D array
Example
#include <stdio.h>
int main() {
int arr[2][3] = { {5, 6, 7}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", arr[i][j]);
}
}
return 0;
}
Output
5
6
7
3
6
8
3D array
Example
#include <stdio.h>
int main() {
// Define a 3D array
int array3D[2][3][4];
// Initialize the 3D array
int value = 1;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
array3D[i][j][k] = value++;
}
}
}
// Traverse and display the 3D array
printf("Traversing 3D Array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("array3D[%d][%d][%d] = %d\n", i, j, k, array3D[i][j][k]);
}
}
}
return 0;
}
Output
Traversing 3D Array:
array3D[0][0][0] = 1
array3D[0][0][1] = 2
array3D[0][0][2] = 3
array3D[0][0][3] = 4
array3D[0][1][0] = 5
array3D[0][1][1] = 6
array3D[0][1][2] = 7
array3D[0][1][3] = 8
array3D[0][2][0] = 9
array3D[0][2][1] = 10
array3D[0][2][2] = 11
array3D[0][2][3] = 12
array3D[1][0][0] = 13
array3D[1][0][1] = 14
array3D[1][0][2] = 15
array3D[1][0][3] = 16
array3D[1][1][0] = 17
array3D[1][1][1] = 18
array3D[1][1][2] = 19
array3D[1][1][3] = 20
array3D[1][2][0] = 21
array3D[1][2][1] = 22
array3D[1][2][2] = 23
array3D[1][2][3] = 24
Input and Output Multidimensional Array Elements in C
We can use the scanf()
function to take inputs of an array from the user.
2D array
Example
#include <stdio.h>
int main() {
int marks[2][3];
// Prompt user to enter grades
for (int i = 0; i < 2; i++)
{
printf("Enter the marks for student %d:\n", i + 1);
for (int j = 0; j < 3; j++)
{
printf("Subject %d: ", j + 1);
scanf("%d", &marks[i][j]);
}
}
printf("The marks entered are:\n");
// Display entered grades
for (int i = 0; i < 2; i++)
{
printf("Student %d: ", i + 1);
for (int j = 0; j < 3; j++)
{
printf("Subject %d: %d\n", j + 1, marks[i][j]);
}
}
return 0;
}
The above program in the C Playground is to print the marks of two students in 3 subjects. The first for
loop indicates the student number. The 2nd for
loop is used to take the marks of the 3 subjects for the student in the first for
loop from the user. In the same way, the two for
loops work for printing the output
Output
Enter the marks for student 1:
Subject 1: 85
Subject 2: 92
Subject 3: 78
Enter the marks for student 2:
Subject 1: 75
Subject 2: 88
Subject 3: 91
The marks entered are:
Student 1: Subject 1: 85
Student 1: Subject 2: 92
Student 1: Subject 3: 78
Student 2: Subject 1: 75
Student 2: Subject 2: 88
Student 2: Subject 3: 91
3D array
Example
/* C Program to dempnstarte a 3D array whose values are taken from the user */
#include <stdio.h>
int main()
{
int arr[2][3][2];
printf("Enter the values as given by the user: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &arr[i][j][k]);
}
}
}
// Printing values with the proper index.
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
Output
Enter the values as given by the user:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying values:
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[0][2][0] = 5
arr[0][2][1] = 6
arr[1][0][0] = 7
arr[1][0][1] = 8
arr[1][1][0] = 9
arr[1][1][1] = 10
arr[1][2][0] = 11
arr[1][2][1] = 12
Summary
This was all about multidimensionalarrays
in C. With this, the topic of arrays in C gets completed. Now, the only thing is to implement whatever learned till now. You must know where and what kind of array
is required. If you're interested in more tips and guidance, you may also consider a C Certification, which can validate your skills and enhance your credibility in the field.