Three Dimensional Array In C
A three-dimensional array is an array of arrays of arrays. It is a very powerful way to organize complex data. In simple terms, a 3D array is like a group of 2D tables stacked on top of each other, where each table is called a block. 3D is a structure that holds data in three levels – blocks, rows, and columns. Here is a general syntax for declaring a 3D array in C:
int array_name[blocks][rows][columns];
- blocks: The number of 2D tables.
- rows: The number of rows in each block.
- columns: The number of columns in each block.
SImple diagram of 3-D array ([4][2][3]) is given under

Declaring a 3D Array
Let’s take a look at an example declaration of a 3D array:
int arr[4][2][3];
Here, arr
is a 3D array with:
- 4 blocks: This means there are 4 different 2D tables.
- 2 rows: Each table has 2 rows.
- 3 columns: Each table has 3 columns.
Initializing a 3D Array
You can initialize the 3D array when declaring it. Here’s an example:
int arr[4][2][3] = {
{
{0, 1, 2}, // Block 0, Row 0
{3, 4, 5} // Block 0, Row 1
},
{
{6, 7, 8}, // Block 1, Row 0
{9, 10, 11} // Block 1, Row 1
},
{
{12, 13, 14}, // Block 2, Row 0
{15, 16, 17} // Block 2, Row 1
},
{
{18, 19, 20}, // Block 3, Row 0
{21, 22, 23} // Block 3, Row 1
}
};
Accessing Elements in a 3D Array
To access an element in a three-dimensional array, you need three indices – one for the block, one for the row, and one for the column. For example, to access the element in Block 1, Row 0, Column 2, you would use:
arr[1][0][2];
Example: Looping Through a 3D Array
To print all the elements of a 3D array, you can use nested for
loops. Here’s how it works:
#include <stdio.h>
int main() {
int arr[4][2][3] = {
{
{0, 1, 2}, // Block 0, Row 0
{3, 4, 5} // Block 0, Row 1
},
{
{6, 7, 8}, // Block 1, Row 0
{9, 10, 11} // Block 1, Row 1
},
{
{12, 13, 14}, // Block 2, Row 0
{15, 16, 17} // Block 2, Row 1
},
{
{18, 19, 20}, // Block 3, Row 0
{21, 22, 23} // Block 3, Row 1
}
};
// Loop through blocks
for (int i = 0; i < 4; i++) {
// Loop through rows
for (int j = 0; j < 2; j++) {
// Loop through columns
for (int k = 0; k < 3; k++) {
printf(“arr[%d][%d][%d] = %d\n”, i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
output
arr[0][0][0] = 0
arr[0][0][1] = 1
arr[0][0][2] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[0][1][2] = 5
arr[1][0][0] = 6
arr[1][0][1] = 7
arr[1][0][2] = 8
arr[1][1][0] = 9
arr[1][1][1] = 10
arr[1][1][2] = 11
arr[2][0][0] = 12
arr[2][0][1] = 13
arr[2][0][2] = 14
arr[2][1][0] = 15
arr[2][1][1] = 16
arr[2][1][2] = 17
arr[3][0][0] = 18
arr[3][0][1] = 19
arr[3][0][2] = 20
arr[3][1][0] = 21
arr[3][1][1] = 22
arr[3][1][2] = 23
3-D array: Address Calculation of Any Element
A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using three subscripts:
- Block size
- Row size
- Column size
More dimensions in an array mean more data can be stored in that array.
To find the address of any element in 3-Dimensional arrays there are the following two ways-
- Row Major Order
- Column Major Order
3D Array: Row Major Order
In row-major order, the elements of a 3D array are stored one row at a time within each block. Once all rows in a block are stored, it moves to the next block.
The formula for finding the address of an element in a 3D array in row-major order is:
Address of A[block][row][col] = Base + Size* {(total_rows * total_cols * (Block-SB) + +(row−SR) * total_cols+(col−SC)}
Where:
- Base = Starting address of the array
- Size = Size (in bytes) of each element in the array
- Block, row, col = Position of the element within the block, row, and column, respectively
- SB, SR, SC = Starting (lower) indices of the blocks, rows, and columns
- total_rows = Total number of rows
- total_cols = Total number of columns
Question:
Given a 3D array arr[3:7, -2:2, 0:4] with:
- Base address Base = 500
- Size of each element Size = 3 bytes
- Find the address of arr[6][0][3] using row-major order.
Solution:
Given:
- Base = 500
- Size = 3 bytes
- Block = 6
- row = 0
- col = 3
- SB = 3, SR = -2, SC = 0
- total_rows = 2−(−2)+1=52 – (-2) + 1 = 52−(−2)+1=5
- total_cols = 4−0+1=54 – 0 + 1 = 54−0+1=5
Formula:
Address of A[block][row][col] = Base + Size* (total_rows * total_cols * (Block-SB) + +(row−SR) * total_cols+(col−SC))
Solution:
Address of A[6][0][3]=500+3×((6−3)×5×5+(0−(−2))×5+(3−0))
= 500+3 *(3*5*5++2*5+3)
= 500+3*(75+10+3)
= 500+3*88
= 500+264
= 764
The address of arr[6][0][3] in row-major order is 764.
3D Array: Column Major Order
In column-major order, the elements of a 3D array are stored one column at a time within each block. Once all columns in a block are stored, it moves to the next block.
The formula for finding the address of an element in a 3D array in column-major order is:
Address of A[block][row][col]= Base+Size×(total_rows×total_blocks×(col−SC)+(row−SR)×total_blocks+(Block−SB))
Where:
- Base = Starting address of the array
- Size = Size (in bytes) of each element in the array
- Block, row, col = Position of the element within the block, row, and column, respectively
- SB, SR, SC = Starting (lower) indices of the blocks, rows, and columns
- total_rows = Total number of rows
- total_blocks = Total number of blocks
Question:
Given a 3D array arr[2:6, -3:1, 1:5] with:
- Base address Base = 600
- Size of each element Size = 4 bytes
- Find the address of arr[5][0][4] using column-major order.
Solution:
Given:
- Base = 600
- Size = 4 bytes
- Block = 5
- row = 0
- col = 4
- SB = 2, SR = -3, SC = 1
- total_rows = 1−(−3)+1=51 – (-3) + 1 = 51−(−3)+1=5
- total_blocks = 6−2+1=56 – 2 + 1 = 56−2+1=5
Formula for Column Major Order:
Address of A[block][row][col] = Base+Size*{(total_rows*total_blocks*(col−SC)+(row−SR)*total_blocks+(Block−SB)}
Solution:
Address of A[5][0][4]=600+4×(5×5×(4−1)+(0−(−3))×5+(5−2))
Address of A[5][0][4]==600+4×(5×5×3+3×5+3)
Address of A[5][0][4]==600+4×(75+15+3)
Address of A[5][0][4]== 600 + 4 × 93
Address of A[5][0][4]==600+372
Address of A[5][0][4]==972
Answer:
The address of arr[5][0][4] in column-major order is 972.
Why Use a Three-Dimensional Array?
Three-dimensional arrays are useful when you need to represent data in multiple levels. For example:
- Game Development: Representing a 3D game map, where each block can be a set of coordinates.
- Data Analysis: Managing large sets of related data, such as time-series data across different locations.