⚙️ C programming basics · Class 10 · Lesson 8 of 18 · Free
2D and Multi-Dimensional Arrays in C – Tables, Grids and Game Boards
⏱ About 35 minutes · ▶ run real C here — nothing to install · no sign-in
In this lesson you will
- Create, fill and print a 2D array with nested loops
- Find row and column totals and pass a 2D array to a function
- Use 2D char arrays for lists of names and grids like seat maps
A class marks register is a table: one row per student, one column per subject. A train coach is a grid of seats, and a tic-tac-toe board is 3 rows of 3 boxes. A 2D array in C stores exactly this kind of table.
What is a 2D array in C?
int marks[3][4] is an array of 3 rows, and each row is an array of 4 ints. You reach one box with two indexes: marks[row][col]. Both start at 0.
To visit every box, use a loop inside a loop: the outer loop picks a row, the inner loop walks along its columns. This is called a nested loop.
In memory, C stores row 0 first, then row 1, then row 2, all side by side. This is called row-major order. So the whole array is 3 x 4 = 12 ints.
Try it: change marks[1][2] to 100 before the loops and run again.
Maths Sci Eng Hindi Roll 1: 78 85 92 70 Roll 2: 65 72 58 81 Roll 3: 90 94 88 97 Boxes: 12, bytes: 48
💡 %6d prints each number in a 6-wide column, so the table lines up. The byte count is for 4-byte ints, as on this compiler.
How do you find row and column totals in a 2D array?
A row total is one student's total: fix the row, add along the columns. A column total is one subject's total for the class: fix the column, add down the rows.
Notice where each sum starts at 0. The student total resets for every row, inside the outer loop.
Try it: add a fourth student row {55, 60, 75, 68} and change 3 to 4 in the loops and in the average.
Roll 1 total: 325 Roll 2 total: 276 Roll 3 total: 369 Maths class average: 77.7 Science class average: 83.7 English class average: 79.3 Hindi class average: 82.7
How do you pass a 2D array to a function in C?
When you pass a 2D array, C passes a pointer to its first row (like 1D arrays in Lesson 3). To find marks[r][c], C must know how long each row is. So the parameter must give the column count: int m[][COLS].
The row count can be left empty, so you pass it as a separate number. A #define constant keeps the column count the same everywhere.
Here a function finds the topper: the row with the biggest total.
Try it: change Roll 2's marks so they become the topper.
Topper: Roll 3 with 369 marks
💡 C99 also allows int m[rows][cols] with sizes passed first, called a variable-length array. The COLS way works everywhere.
How do you store a list of names in C?
A string is a char array (Lesson 3), so a list of strings can be a 2D char array: char names[5][20] holds 5 names of up to 19 letters each.
names[i] is one whole name, and names[i][0] is its first letter. You copy names with strcpy and compare with strcmp.
This program sorts the class list alphabetically with a simple bubble sort: compare neighbours and swap if they are in the wrong order. You will study sorting in Lesson 15.
Try it: add a sixth name. Remember to change 5 to 6 and the array size too.
1. Aman (starts with A) 2. Ananya (starts with A) 3. Kabir (starts with K) 4. Riya (starts with R) 5. Zoya (starts with Z)
How do you make a seat map or game board with a 2D array in C?
A 2D char array is a natural grid. Here '.' is a free seat and 'X' is booked in a small 4 x 5 coach.
The user types a row and a seat number. The program checks they are inside the grid BEFORE touching the array, because C does not check indexes for you.
Try it: change the inputs to book seat 2 3 twice and see what happens.
Book row and seat (1-based, 0 0 to stop): 1 1 2 3 4 5 9 1 No such seat 0 0 X . . . . . . X . . . . . . . . . . . X
🤔 Predict the output
Think like C: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", a[1][0] + a[0][2]);Question 2. What does this print?
int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += m[i][i];
}
printf("%d\n", sum);Question 3. What does this print?
int t[2][3] = {{7}, {8, 9}};
printf("%d %d %d\n", t[0][1], t[1][1], t[1][2]);🧩 Your challenge
Write char winner(char b[3][3]) for tic-tac-toe. It returns 'X' or 'O' if that player has 3 in a row (any row, column or diagonal), else '-'. The program checks two boards. Expected output: Board 1: X wins Board 2: no winner
Board 1: X wins Board 2: no winner
⚠️ Common mistakes (and how to fix them)
❌ Wrong
#include <stdio.h>
void show(int m[][]) {
printf("%d\n", m[0][0]);
}
int main(void) {
int marks[2][3] = {{1, 2, 3}, {4, 5, 6}};
show(marks);
return 0;
}
Line 3: error: array has incomplete element type 'int[]'
✅ Right
#include <stdio.h>
void show(int m[][3]) {
printf("%d\n", m[0][0]);
}
int main(void) {
int marks[2][3] = {{1, 2, 3}, {4, 5, 6}};
show(marks);
return 0;
}
C needs the column count to find m[r][c] in memory. Only the first size may be left empty.
❌ Wrong
int m[2][3] = {{10, 20, 30}, {1, 2, 3}};
int total = 0;
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
total += m[r][c];
}
printf("Row %d: %d\n", r, total);
}Compiles and runs, but prints: Row 0: 60 ⏎ Row 1: 66 ⏎
✅ Right
int m[2][3] = {{10, 20, 30}, {1, 2, 3}};
for (int r = 0; r < 2; r++) {
int total = 0;
for (int c = 0; c < 3; c++) {
total += m[r][c];
}
printf("Row %d: %d\n", r, total);
}The total was never reset, so row 1 also counts row 0. Start each row's total at 0 inside the outer loop.
❌ Wrong
int n = 3;
int grid[n][n] = {0};
printf("%d\n", grid[1][1]);Line 2: error: variable-sized object may not be initialized
✅ Right
#define N 3
int grid[N][N] = {0};
printf("%d\n", grid[1][1]);An array whose size is a variable (a variable-length array) cannot have an = { } initializer in C17. Use a constant size, or fill it with loops.
📌 Remember
- int a[R][C] is R rows of C items; reach one with a[row][col], both from 0.
- Use nested loops: the outer loop for rows, the inner loop for columns.
- C stores 2D arrays row by row in one block of memory (row-major order).
- When passing a 2D array to a function, give the column count: int m[][COLS].
- char names[N][LEN] holds a list of strings; char grids are great for seats and boards.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a 1D and 2D array in C?
A 1D array is a single row of values reached with one index, like marks[3]. A 2D array is a table reached with two indexes, like marks[2][3] for row 2, column 3.
Can C have 3D arrays?
Yes. int marks[2][40][5] could store 2 sections, 40 students each, 5 subjects each. You use three indexes and three nested loops. More than three dimensions are allowed but rarely needed.
Why must I give the column size when passing a 2D array?
C finds m[r][c] by jumping r whole rows and then c items. To jump a whole row, it must know how many items a row has. The number of rows is not needed for this.
How do I take input into a 2D array in C?
Use two nested loops and scanf("%d", &m[r][c]) inside the inner loop. Print a prompt that shows which row and column the user is typing.