A multidimensional array is an array whose elements are themselves arrays. In C, a two-dimensional array is commonly used to represent data arranged in rows and columns, such as a matrix or a table.
#include <stdio.h>
int main(void)
{
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 3; col++)
{
printf("%d ", matrix[row][col]);
}
printf("\n");
}
return 0;
}
Example Output #
10 20 30
40 50 60
Explanation #
The declaration:
int matrix[2][3];
creates a two-dimensional array containing two rows and three columns, for a total of six integer elements.
The first index selects a row, and the second index selects a column:
matrix[0][1] = 20;
matrix[1][2] = 60;
C stores the elements of a multidimensional array in row-major order. All elements of the first row are stored before the elements of the second row.
Multidimensional Array Declarations #
| Declaration | Structure | Total elements |
|---|---|---|
int a[3][4]; |
3 rows × 4 columns | 12 |
int a[2][3]; |
2 rows × 3 columns | 6 |
int a[2][3][4]; |
2 × 3 × 4 dimensions | 24 |
For a three-dimensional array:
int values[2][3][4];
three indices are required to access an individual element:
values[1][2][3] = 100;
Each additional dimension introduces another level of indexing. Multidimensional arrays are useful for matrices, grids, tabular data, and other fixed-size structures.