C Multi-Dimensional Array
A multi-dimensional array uses two or more indexes to organize same-type elements across multiple dimensions.
A two-dimensional array has rows and columns. A three-dimensional array adds another dimension, which you can imagine as layers. If you need the simpler form first, review Two Dimensional Array.
Quick Answer
A multi-dimensional C array uses more than one index. For example, int data[2][2][3]; is a three-dimensional array. One element can be accessed with data[1][0][2]. Each index selects a position in one dimension.
What Is a Multi-Dimensional Array in C?
A multi-dimensional array is an array with more than one dimension.
A one-dimensional array uses one index. A two-dimensional array uses two indexes. A three-dimensional array uses three indexes.
All elements still have the same type. The extra dimensions only change how the elements are organized and accessed.
C Multi-Dimensional Array Syntax
A three-dimensional array can be declared like this:
data_type array_name[size1][size2][size3];
For example:
int data[2][2][3];
This creates a three-dimensional array with 2 × 2 × 3 = 12 integer elements.
C Multi-Dimensional Array Example
This program creates a 2 × 2 × 3 array and uses three nested loops to print every element.
#include <stdio.h>
int main(void) {
int data[2][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
int layer;
int row;
int column;
for (layer = 0; layer < 2; layer++) {
printf("Layer %d:\n", layer);
for (row = 0; row < 2; row++) {
for (column = 0; column < 3; column++) {
printf("%d ", data[layer][row][column]);
}
printf("\n");
}
}
return 0;
}
Output
Layer 0:
1 2 3
4 5 6
Layer 1:
7 8 9
10 11 12
The first loop selects a layer. The second loop selects a row inside that layer. The third loop selects a column inside that row.
Initializing a Multi-Dimensional Array
Braces can be nested to match the dimensions:
int data[2][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
The outer groups represent the first dimension. Inside them are groups for the next dimensions.
Accessing a Multi-Dimensional Array Element
Use one index for each dimension:
data[1][0][2]
In the example, this selects the value 9. Using a layer-row-column view, the indexes are layer 1, row 0, and column 2.
Changing an Element
You can assign a new value by using all required indexes:
data[0][1][2] = 99;
This changes only the selected element.
Traversing a Multi-Dimensional Array
A three-dimensional array is commonly traversed with three nested loops.
Each loop controls one dimension:
for (layer = 0; layer < 2; layer++) {
for (row = 0; row < 2; row++) {
for (column = 0; column < 3; column++) {
printf("%d ", data[layer][row][column]);
}
}
}
Valid Index Ranges
For int data[2][2][3], the first index can be 0 or 1.
The second index can also be 0 or 1. The third index can be 0, 1, or 2.
Using an index outside a valid range causes undefined behavior.
Common Multi-Dimensional Array Mistakes
| Mistake | Correct Idea |
|---|---|
| Using too few indexes when trying to select one scalar element. | Use one index for each dimension, such as data[1][0][2]. |
Using an invalid index such as data[2][0][0] when the first size is 2. |
The valid first indexes are 0 and 1. |
| Using only one or two loops to visit every element of a three-dimensional array directly. | Three nested loops are the common beginner pattern for three dimensions. |
Short Practice
- Declare a three-dimensional integer array named
boxwith sizes2,3, and4. - How many integer elements are in
int box[2][3][4]? - In
data[1][0][2], identify the first, second, and third index values.
FAQs
What is a multi-dimensional array in C?
A multi-dimensional array in C is an array that uses two or more indexes to organize same-type elements across multiple dimensions.
How do you declare a three-dimensional array in C?
Use three bracketed sizes, such as int data[2][2][3];.
What does data[1][0][2] mean in C?
It selects the element at first index 1, second index 0, and third index 2. In a layer-row-column view, that can be read as layer 1, row 0, column 2.
How many loops are commonly used to traverse a three-dimensional array?
Three nested loops are commonly used: one for the first dimension, one for the second, and one for the third.
Summary
A multi-dimensional C array uses two or more indexes to organize same-type elements. A three-dimensional array uses three indexes, which can be imagined as layer, row, and column positions. You can initialize, access, update, and traverse the elements with one index or loop for each dimension.