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.
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 4ints, 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
m[i][j], notm[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
1 2 3 4 5 6 sizeof m = 24 bytes
Row and column totals
student 1 total 163 student 2 total 134 student 3 total 178 subject totals: 232 243
More examples
Transpose and multiply
transpose of A: 1 4 2 5 3 6 A x B: 58 64 139 154
Step-by-step explanation
printuses 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 rowiand B's columnj. - 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:
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/colori/jconsistently. - 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
Prefer
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
Prefer
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
doublearray 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
- 1Check whether a 3 × 3 matrix is symmetric (equal to its transpose).
- 2Print 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?