Skip to content

Module 5 · Arrays and Strings — topic 2 of 3

Multidimensional Arrays

Two-dimensional arrays represent tables, grids and matrices. Learn row-major layout, initialisation, nested traversal, matrix operations and passing 2D arrays to functions.

Beginner 15 min read·Lesson 20 of 37 in C Programming

Learning objectives

  • Declare and initialise 2D arrays
  • Traverse rows and columns with nested loops
  • Implement matrix addition, transpose and multiplication

Definition

A multidimensional array in C is an array whose elements are themselves arrays. int m[3][4] is an array of 3 elements, each an array of 4 ints, stored contiguously in row-major order.

Introduction

A marks register has students as rows and subjects as columns. A chessboard is 8 × 8. A greyscale image is height × width. All of these map directly onto two-dimensional arrays.

In memory, a 2D array is still one contiguous block: row 0, then row 1, and so on. Knowing this row-major layout explains how C computes addresses and why traversing row by row is faster than column by column.

Why it matters

Matrix problems (addition, multiplication, transpose, spiral printing, searching a sorted matrix) are standard in exams and interviews.

Passing 2D arrays to functions has syntax rules that trip up many students; learning them once saves a lot of compiler errors.

Detailed explanation

Declare as type name[ROWS][COLS];. Access with two indexes: m[i][j] is row i, column j.

Initialise with nested braces: int m[2][3] = {{1, 2, 3}, {4, 5, 6}};. Only the first dimension may be omitted: int m[][3] = {...}.

The address of m[i][j] is base + (i * COLS + j) * sizeof(int). That is why the compiler must know COLS — the second dimension — whenever it indexes the array.

When passing to a function, every dimension except the first must be given: void print(int m[][4], int rows) or, in C99, with variable sizes: void print(int rows, int cols, int m[rows][cols]).

Matrix multiplication C = A × B requires A's columns to equal B's rows; C[i][j] = Σ A[i][k] × B[k][j] — three nested loops.

Syntax

C
int grid[ROWS][COLS];
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
m[1][2] = 60; /* row 1, column 2 */
void show(int rows, int cols, int a[rows][cols]); /* C99 VLA parameter */
void show4(int a[][4], int rows); /* fixed column count */
  • m[i][j], not m[i, j] — the comma version is the comma operator.
  • The innermost brace lists are rows.
  • Iterate rows in the outer loop for the best cache performance.

Code examples

Printing a 2D array

C
#include <stdio.h>
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++)
printf("%3d", m[i][j]);
printf("\n");
}
printf("sizeof m = %zu bytes\n", sizeof m);
return 0;
}
Output
  1  2  3
  4  5  6
sizeof m = 24 bytes

Row and column totals

C
#include <stdio.h>
int main(void) {
int marks[3][2] = {{78, 85}, {64, 70}, {90, 88}};
int col[2] = {0};
for (int s = 0; s < 3; s++) {
int row = 0;
for (int sub = 0; sub < 2; sub++) {
row += marks[s][sub];
col[sub] += marks[s][sub];
}
printf("student %d total %d\n", s + 1, row);
}
printf("subject totals: %d %d\n", col[0], col[1]);
return 0;
}
Output
student 1 total 163
student 2 total 134
student 3 total 178
subject totals: 232 243

More examples

Transpose and multiply

C
#include <stdio.h>
void print(int r, int c, int m[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) printf("%4d", m[i][j]);
printf("\n");
}
}
int main(void) {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int t[3][2], p[2][2] = {{0}};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
t[j][i] = a[i][j];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 3; k++)
p[i][j] += a[i][k] * b[k][j];
printf("transpose of A:\n");
print(3, 2, t);
printf("A x B:\n");
print(2, 2, p);
return 0;
}
Output
transpose of A:
   1   4
   2   5
   3   6
A x B:
  58  64
 139 154

Step-by-step explanation

  • print uses a C99 variable-length array parameter so it works for any size.
  • Transpose: element (i, j) of A becomes element (j, i) of T.
  • For the product, p[i][j] accumulates the dot product of A's row i and B's column j.
  • p[0][0] = 1×7 + 2×9 + 3×11 = 58; p[1][1] = 4×8 + 5×10 + 6×12 = 154.

Expected output

Running “Transpose and multiply” prints exactly:

TextOutput
transpose of A:
1 4
2 5
3 6
A x B:
58 64
139 154

Practical applications

  • Spreadsheets and timetables are 2D grids.
  • Graphics transforms multiply coordinates by matrices.
  • Board games (tic-tac-toe, chess, sudoku) store the board in a 2D array.

Best practices

  • Name loop variables row/col or i/j consistently.
  • Traverse in row-major order (rows outer, columns inner).
  • Use constants or VLA parameters for dimensions instead of repeating numbers.

Common mistakes

Using a comma between indexes

Avoid

m[1, 2] = 5;

Prefer

m[1][2] = 5;

m[1, 2] evaluates 1, 2 to 2 and means m[2] — a whole row, not an element.

Omitting the column size in a parameter

Avoid

void show(int m[][], int r, int c);

Prefer

void show(int r, int c, int m[r][c]);

The compiler needs the column count to compute m[i][j]'s address.

Important notes

  • An array of pointers (int *rows[3]) and a true 2D array (int m[3][4]) are indexed the same way but have different memory layouts and are not interchangeable as parameters.
  • For large matrices, allocate on the heap; a 1000 × 1000 double array is 8 MB, more than many default stacks.

Key takeaways

  • int m[R][C] is R rows of C elements, stored row by row.
  • Every dimension except the first is required in a parameter.
  • Matrix multiplication uses three nested loops.

Try it yourself

  1. 1
    Check whether a 3 × 3 matrix is symmetric (equal to its transpose).
  2. 2
    Print a 4 × 4 matrix in spiral order.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

6 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • ModerateHow is `int m[3][4]` laid out in memory?
  • ModerateWhat is printed?
Practise this topic