A multidimensional array in C is an array that contains more than one dimension. A 2D array has rows and columns, while a 3D array can be viewed as multiple 2D arrays or layers. Multidimensional arrays are useful for storing data such as matrices, tables, cubes, marks, and other structured information.
arr[1][2][3] uses three indexes.
A multidimensional array is an array having two or more dimensions. It allows us to store data in multiple levels.
int numbers[2][3];
The above array is a two-dimensional array with 2 rows and 3 columns.
A dimension represents one level of indexing in an array.
The number of dimensions determines how many indexes are needed to access an element.
A 3D array is an array containing multiple 2D arrays.
int arr[2][3][4];
This array has 2 layers, each layer has 3 rows, and each row has 4 columns.
The general syntax for declaring a 3D array is:
data_type array_name[size1][size2][size3];
Example:
int marks[2][3][4];
Here, 2, 3, and 4 represent the three dimensions.
A 3D array can be imagined as a collection of 2D tables placed one after another.
int data[2][2][3];
This contains 2 layers. Each layer contains 2 rows and each row contains 3 elements.
Three indexes are used to access an element of a 3D array.
int arr[2][3][4];
arr[0][1][2] = 50;
The indexes represent the layer, row, and column.
Array indexes in C start from zero. For an array declared as:
int arr[2][3][4];
The valid indexes are:
A 3D array can be initialized when it is declared.
int arr[2][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
Use three indexes when reading a value from a 3D array.
printf("%d", arr[1][0][2]);
This accesses the element at layer 1, row 0, and column 2.
An individual element can be changed using its indexes.
arr[0][1][2] = 100;
Only that particular element is changed.
Three nested loops are commonly used to process all elements of a 3D array.
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = 0; k < 4; k++)
{
printf("%d ", arr[i][j][k]);
}
}
}
Usually, one loop variable is used for each dimension.
i → layer
j → row
k → column
This makes the nested loops easier to understand.
The following program displays all values of a 3D array.
#include <stdio.h>
int main()
{
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Input can also be taken using three nested loops.
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
scanf("%d", &arr[i][j][k]);
}
}
}
A nested loop can be used to calculate the sum of all elements.
int sum = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
sum += arr[i][j][k];
}
}
}
You can count elements satisfying a condition.
int count = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
if(arr[i][j][k] > 10)
{
count++;
}
}
}
}
A value can be searched by checking every element.
int found = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
if(arr[i][j][k] == 7)
{
found = 1;
}
}
}
}
The largest element can be found by comparing every value.
int largest = arr[0][0][0];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
if(arr[i][j][k] > largest)
{
largest = arr[i][j][k];
}
}
}
}
The smallest value can be found in a similar way.
int smallest = arr[0][0][0];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
if(arr[i][j][k] < smallest)
{
smallest = arr[i][j][k];
}
}
}
}
C stores array elements in contiguous memory according to the array's layout. For multidimensional arrays, the rightmost dimension varies fastest when elements are laid out in memory.
int arr[2][2][3];
Understanding this becomes useful when working with pointers and functions.
A 3D array can represent data arranged in layers, rows, and columns. For example, a small cube of values can be represented as:
int cube[3][3][3];
Here there are 3 layers, 3 rows per layer, and 3 columns per row.
A multidimensional array can be passed to a function. The trailing dimensions generally need to be specified in the parameter type.
void display(int arr[][2][3])
{
// process array
}
The first dimension may be omitted in the parameter declaration.
Here is a simple function that displays a 3D array.
void display(int arr[][2][2])
{
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("%d ", arr[i][j][k]);
}
}
}
}
The sizeof operator can be used to determine the total size of an
array when the array itself is in scope.
int arr[2][3][4];
printf("%zu", sizeof(arr));
The result is the total number of bytes occupied by the complete array.
For a fully declared array, the total number of elements can be calculated by dividing its total size by the size of one element.
int arr[2][3][4];
size_t total = sizeof(arr) / sizeof(arr[0][0][0]);
The result is 24 elements.
Always use indexes within the declared limits.
int arr[2][3][4];
The expression arr[2][0][0] is invalid because the first dimension
has valid indexes only from 0 to 1.
Accessing outside the bounds of an array causes undefined behavior.
Loop limits must match the array dimensions.
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++)
{
printf("%d ", arr[i][j][k]);
}
}
}
Using larger limits can access elements outside the array.
#include <stdio.h>
int main()
{
int arr[2][2][2];
int sum = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("Enter value: ");
scanf("%d", &arr[i][j][k]);
sum += arr[i][j][k];
}
}
}
printf("Sum = %d", sum);
return 0;
}
This program takes input for every element of a 3D array and calculates the total.
Multidimensional arrays can be used in many programming problems.
Create a C program that:
Use three nested loops to process all elements.
sizeof can be used to calculate the total size of an array when it is in scope.Question: How many indexes are required to access an element of a 3D array?