C course menu
Multidimensional Arrays in C: 2D Arrays, Matrices and Passing Them to Functions
Learn two-dimensional arrays in C: declaring and initialising a 2D array, row-major memory layout, nested loops over rows and columns, row and column totals, matrix addition and multiplication, and passing 2D arrays to functions.
On this page
A two-dimensional array is an array of arrays: a grid of rows and columns, such as a table of marks or a matrix.
Declaring and initialising#
#include <stdio.h>
int main(void) {
int marks[3][4] = {
{78, 85, 90, 66}, /* student 0 */
{88, 72, 95, 81}, /* student 1 */
{59, 64, 70, 75}, /* student 2 */
};
printf("%d\n", marks[1][2]); /* row 1, column 2 */
printf("%zu bytes\n", sizeof marks);
return 0;
}95
48 bytesint marks[3][4] has 3 rows of 4 ints: 12 elements, 48 bytes with a 4-byte int. The first index is the row and the second is the column.
Memory layout#
C stores 2D arrays in row-major order: all of row 0, then all of row 1, and so on, in one continuous block. Looping row by row (outer loop over rows, inner over columns) follows memory in order, which is the fastest way to traverse it.
Nested loops#
int marks[3][4] = {
{78, 85, 90, 66},
{88, 72, 95, 81},
{59, 64, 70, 75},
};
for (int r = 0; r < 3; r++) {
int total = 0;
for (int c = 0; c < 4; c++) {
printf("%4d", marks[r][c]);
total += marks[r][c];
}
printf(" | total %d\n", total);
}
printf("subject averages:");
for (int c = 0; c < 4; c++) {
int sum = 0;
for (int r = 0; r < 3; r++) sum += marks[r][c];
printf(" %.1f", sum / 3.0);
}
printf("\n"); 78 85 90 66 | total 319
88 72 95 81 | total 336
59 64 70 75 | total 268
subject averages: 75.0 73.7 85.0 74.0Passing a 2D array to a function#
Every dimension except the first must be given in the parameter, so the compiler can work out where each row starts:
#include <stdio.h>
#define COLS 3
void print_matrix(int m[][COLS], int rows) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < COLS; c++) printf("%3d", m[r][c]);
printf("\n");
}
}
int main(void) {
int a[2][COLS] = {{1, 2, 3}, {4, 5, 6}};
print_matrix(a, 2);
return 0;
} 1 2 3
4 5 6C99 also allows variable-length array parameters, where the sizes come first: void print(int rows, int cols, int m[rows][cols]).
Matrix multiplication#
For an n×m matrix A and an m×p matrix B, each element of the result is a row of A multiplied by a column of B:
#include <stdio.h>
int main(void) {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int c[2][2] = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];
for (int i = 0; i < 2; i++)
printf("%d %d\n", c[i][0], c[i][1]);
return 0;
}58 64
139 154Practice#
Practice exercise
Write a program that prints the transpose of int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; (rows become columns) and the sum of the main diagonal of int sq[3][3] = {{2, 0, 1}, {3, 5, 7}, {4, 6, 9}};.
Show solution
#include <stdio.h>
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
int t[3][2];
for (int r = 0; r < 2; r++)
for (int c = 0; c < 3; c++)
t[c][r] = m[r][c];
for (int r = 0; r < 3; r++)
printf("%d %d\n", t[r][0], t[r][1]);
int sq[3][3] = {{2, 0, 1}, {3, 5, 7}, {4, 6, 9}};
int trace = 0;
for (int i = 0; i < 3; i++) trace += sq[i][i];
printf("diagonal sum %d\n", trace);
return 0;
}1 4
2 5
3 6
diagonal sum 16Key takeaway#
Finished this lesson?