Nearby lessons

58 of 124

C - Two Dimensional Array

The 2D array is a table of rows and columns — C's matrix type. Learn declaration, initialisation, nested-loop traversal, row-major memory layout, and matrix addition and transpose.

Declaring a 2D Array

Two sets of brackets: the first is the number of rows, the second the number of columns:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int matrix[3][4]; /* 3 rows, 4 columns = 12 ints */
6 float grid[2][2];
7 char board[3][3];
8 
9 printf("matrix: %zu bytes (%zu ints)\n",
10 sizeof(matrix), sizeof(matrix) / sizeof(int));
11 printf("grid : %zu bytes\n", sizeof(grid));
12 printf("board : %zu bytes\n", sizeof(board));
13 return 0;
14}
Output
matrix: 48 bytes (12 ints)
grid  : 16 bytes
board : 9 bytes

Initialising with Nested Braces

Group each row in its own set of braces. It compiles without the inner braces too, but the nested form documents the shape:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 /* clearest form - one brace group per row */
6 int a[3][4] = {
7 {1, 2, 3, 4},
8 {5, 6, 7, 8},
9 {9, 10, 11, 12}
10 };
11 
12 /* legal but harder to read - filled row by row */
13 int b[2][3] = {1, 2, 3, 4, 5, 6};
14 
15 /* partial - missing values become 0 */
16 int c[2][3] = {{1, 2}, {4}};
17 
18 printf("a[1][2] = %d\n", a[1][2]);
19 printf("b[1][0] = %d\n", b[1][0]);
20 printf("c[0][2] = %d, c[1][1] = %d\n", c[0][2], c[1][1]);
21 return 0;
22}
Output
a[1][2] = 7
b[1][0] = 4
c[0][2] = 0, c[1][1] = 0

Printing a Matrix with Nested Loops

The outer loop selects the row; the inner loop walks that row's columns. The newline goes in the outer loop:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[3][4] = {
6 {1, 2, 3, 4},
7 {5, 6, 7, 8},
8 {9, 10, 11, 12}
9 };
10 int i, j;
11 
12 for (i = 0; i < 3; i++) /* rows */
13 {
14 for (j = 0; j < 4; j++) /* columns */
15 printf("%4d", a[i][j]);
16 printf("\n"); /* end of row */
17 }
18 return 0;
19}
Output
   1   2   3   4
   5   6   7   8
   9  10  11  12

Row-Major Storage

Memory is one-dimensional, so C flattens the table. It stores all of row 0, then all of row 1, and so on — this is called row-major order:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
6 int i, j;
7 
8 for (i = 0; i < 2; i++)
9 for (j = 0; j < 3; j++)
10 printf("a[%d][%d] = %d at %p\n",
11 i, j, a[i][j], (void *) &a[i][j]);
12 return 0;
13}
Output
a[0][0] = 1  at 000000000061FE00
a[0][1] = 2  at 000000000061FE04
a[0][2] = 3  at 000000000061FE08
a[1][0] = 4  at 000000000061FE0C
a[1][1] = 5  at 000000000061FE10
a[1][2] = 6  at 000000000061FE14

The Address Formula

In simple words: a 2D array is a 1D array wearing a disguise. Element a[i][j] sits at position i * columns + j in the flat block.

So a[1][0] in a 3-column array is at flat position 1*3 + 0 = 3 — the fourth element. That is why the column count must be given when passing a 2D array to a function: without it, C cannot compute where a row begins.

Reading a Matrix from the User

Two nested loops with scanf. Remember the & before a[i][j]:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[2][3];
6 int i, j;
7 
8 printf("Enter 6 values for a 2x3 matrix:\n");
9 for (i = 0; i < 2; i++)
10 for (j = 0; j < 3; j++)
11 scanf("%d", &a[i][j]);
12 
13 printf("You entered:\n");
14 for (i = 0; i < 2; i++)
15 {
16 for (j = 0; j < 3; j++) printf("%4d", a[i][j]);
17 printf("\n");
18 }
19 return 0;
20}
Output
Enter 6 values for a 2x3 matrix:
1 2 3 4 5 6
You entered:
   1   2   3
   4   5   6

Matrix Addition

Add matching positions. Both matrices must have identical dimensions:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
6 int b[2][3] = {{7, 8, 9}, {1, 2, 3}};
7 int sum[2][3];
8 int i, j;
9 
10 for (i = 0; i < 2; i++)
11 for (j = 0; j < 3; j++)
12 sum[i][j] = a[i][j] + b[i][j];
13 
14 printf("Sum matrix:\n");
15 for (i = 0; i < 2; i++)
16 {
17 for (j = 0; j < 3; j++) printf("%4d", sum[i][j]);
18 printf("\n");
19 }
20 return 0;
21}
Output
Sum matrix:
   8  10  12
   5   7   9

Transpose — Swapping Rows and Columns

The transpose of an m×n matrix is n×m. Simply write a[i][j] into t[j][i]:

Example08
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
6 int t[3][2];
7 int i, j;
8 
9 for (i = 0; i < 2; i++)
10 for (j = 0; j < 3; j++)
11 t[j][i] = a[i][j]; /* indexes swapped */
12 
13 printf("Transpose (3x2):\n");
14 for (i = 0; i < 3; i++)
15 {
16 for (j = 0; j < 2; j++) printf("%4d", t[i][j]);
17 printf("\n");
18 }
19 return 0;
20}
Output
Transpose (3x2):
   1   4
   2   5
   3   6

Passing a 2D Array to a Function

You may omit the row count, but the column count is mandatory:

Example09
CCode Cell
1#include <stdio.h>
2 
3/* the 3 is required; the row count may be left empty */
4void printMatrix(int a[][3], int rows)
5{
6 int i, j;
7 for (i = 0; i < rows; i++)
8 {
9 for (j = 0; j < 3; j++) printf("%4d", a[i][j]);
10 printf("\n");
11 }
12}
13 
14int main()
15{
16 int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
17 printMatrix(a, 2);
18 return 0;
19}
Output
   1   2   3
   4   5   6

Common Mistakes

  • Writing a[i, j] — that is the comma operator; it evaluates to a[j]. Always use a[i][j].
  • Omitting the column count in a parametervoid f(int a[][]) will not compile.
  • Swapping the loop bounds — using the column count for rows silently reads out of bounds.
  • Transposing in place on a non-square matrix — the result has different dimensions, so you need a second array.
  • Printing the newline in the inner loop — puts every element on its own line.
Row count vs column count: int a[3][4] is 3 rows of 4 columns, so the valid indexes are a[0..2][0..3]. Mixing these up is the most common 2D array bug.
📝 Key Takeaways
  • Syntax: type name[rows][columns].
  • arr[i][j] means row i, column j — both start at 0.
  • C stores 2D arrays in row-major order: row 0 entirely, then row 1.
  • The outer loop walks rows, the inner loop walks columns.
  • When passing a 2D array to a function, the column count is required.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4