C Multidimensional Arrays
An ordinary array is a single row of values. Many kinds of data are naturally laid out in a grid instead: a chessboard, a spreadsheet, the pixels of an image, a seating plan. For these, C lets you create arrays of arrays — multidimensional arrays.
Two-dimensional arrays
A 2D array has rows and columns. Give the number of rows first, then the number of columns:
int matrix[2][3]; // 2 rows, 3 columns = 6 intsYou can initialise it with one inner brace list per row:
#include <stdio.h>
int main(void) {
int matrix[2][3] = {
{1, 4, 2},
{3, 6, 8}
};
printf("Row 0, column 2: %d\n", matrix[0][2]);
printf("Row 1, column 0: %d\n", matrix[1][0]);
matrix[1][1] = 9; // change one element
printf("Row 1, column 1 is now: %d\n", matrix[1][1]);
return 0;
}Row 0, column 2: 2
Row 1, column 0: 3
Row 1, column 1 is now: 9
Remember that both indexes count from zero: matrix[0][2] is the third value of the first row.
Looping through a 2D array
Use two nested loops: the outer one walks the rows, the inner one walks the columns of the current row.
#include <stdio.h>
#define ROWS 3
#define COLS 4
int main(void) {
int seats[ROWS][COLS] = {
{1, 0, 1, 1},
{0, 0, 1, 0},
{1, 1, 1, 1}
};
int taken = 0;
for (int r = 0; r < ROWS; r++) {
printf("Row %c: ", 'A' + r);
for (int c = 0; c < COLS; c++) {
printf("%s", seats[r][c] ? "[X]" : "[ ]");
taken += seats[r][c];
}
printf("\n");
}
printf("%d of %d seats taken\n", taken, ROWS * COLS);
return 0;
}Row A: [X][ ][X][X]
Row B: [ ][ ][X][ ]
Row C: [X][X][X][X]
8 of 12 seats taken
Row and column sums
Working with a grid often means aggregating along one direction. Here we compute totals per student (row) and per subject (column):
#include <stdio.h>
int main(void) {
const char *students[] = {"Ana", "Ben", "Cai"};
int marks[3][3] = {
{80, 72, 91},
{65, 88, 70},
{92, 79, 85}
};
for (int s = 0; s < 3; s++) {
int total = 0;
for (int sub = 0; sub < 3; sub++) {
total += marks[s][sub];
}
printf("%-4s total %d\n", students[s], total);
}
for (int sub = 0; sub < 3; sub++) {
int total = 0;
for (int s = 0; s < 3; s++) {
total += marks[s][sub];
}
printf("Subject %d average %.1f\n", sub + 1, total / 3.0);
}
return 0;
}Ana total 243
Ben total 223
Cai total 256
Subject 1 average 79.0
Subject 2 average 79.7
Subject 3 average 82.0
How it looks in memory
Memory is one-dimensional, so C stores a 2D array row by row (row-major order). int m[2][3] is six consecutive ints: all of row 0, then all of row 1.
| Element | m[0][0] |
m[0][1] |
m[0][2] |
m[1][0] |
m[1][1] |
m[1][2] |
|---|---|---|---|---|---|---|
| Position in memory | 0 | 1 | 2 | 3 | 4 | 5 |
Because of this layout, the inner braces in an initialiser are optional (int m[2][3] = {1,2,3,4,5,6}; works), and looping row-by-row is faster than column-by-column for big arrays, since it reads memory in order.
#include <stdio.h>
int main(void) {
int m[2][3] = {1, 2, 3, 4, 5, 6};
printf("sizeof(m) = %zu bytes\n", sizeof(m));
printf("sizeof(m[0]) = %zu bytes (one row)\n", sizeof(m[0]));
printf("rows = %zu, cols = %zu\n", sizeof(m) / sizeof(m[0]), sizeof(m[0]) / sizeof(m[0][0]));
printf("m[1][0] = %d\n", m[1][0]);
return 0;
}sizeof(m) = 24 bytes
sizeof(m[0]) = 12 bytes (one row)
rows = 2, cols = 3
m[1][0] = 4
More dimensions
You can add as many dimensions as you need. A 3D array could hold several 2D layers, for example the floors of a building:
int building[3][4][5]; // 3 floors, 4 rows, 5 rooms per row
building[2][0][4] = 1; // floor 2, row 0, room 4Each extra dimension multiplies the memory used, so very large multidimensional arrays are usually allocated dynamically.
Tip: When you pass a 2D array to a function, every dimension except the first must be written in the parameter:
void print(int m[][3], int rows).
Test yourself
How many int elements does int grid[4][5]; contain?
- 9
- 20
- 45
- 5
With int m[2][2] = {{1, 2}, {3, 4}};, what is m[1][0]?
- 1
- 2
- 3
- 4
In what order does C store the elements of a 2D array in memory?
- Column by column
- Row by row
- Randomly
- Diagonally