Theory
One student was a line; a class is a grid
The marks array stored one number per student. Reality: each student has FOUR subjects.
Sixty students × four subjects is not a line of boxes, it is a grid: rows and columns, exactly the marksheet a teacher pins on the noticeboard.
C builds grids by giving an array a second index:
int marks[3][4]; /* 3 students, 4 subjects */
If this smells like BCA102's matrices, trust your nose, same grid, now in code.
Theory
The cinema hall
A cinema ticket never says "seat 47"; it says row F, seat 9: first the row, then the seat within it. A 2D array is the cinema hall: marks[1][2] means row 1, seat 2 (both counted from zero). The RC rule from BCA102 survives intact: Rows first, then Columns, like an RC car, and now it drives your code.
Theory
Declaration and initialization
- Declare:
int marks[3][4];reserves 3 × 4 = 12 int boxes. - Initialize, braces inside braces (one inner brace per row):
int marks[2][3] = {
{70, 80, 90}, /* student 0 */
{60, 75, 85} /* student 1 */
};
- Access:
marks[1][2]is student 1 (second), subject 2 (third): 85.
In memory the grid is stored row by row (row-major): all of row 0's boxes first, then row 1's, one long line wearing grid spectacles.
Practical
Walking the grid with nested loops
#include <stdio.h>
int main() {
int marks[2][3] = {{70, 80, 90}, {60, 75, 85}};
int i, j;
for (i = 0; i < 2; i++) { /* each student (row) */
printf("Student %d: ", i);
for (j = 0; j < 3; j++) /* each subject (col) */
printf("%d ", marks[i][j]);
printf("\n");
}
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Read the grid
Using int marks[2][3] = {{70, 80, 90}, {60, 75, 85}}; work out: marks[0][1], marks[1][0], and marks[1][2]. Rows first, from zero.
Show the answer
marks[0][1] = 80 (first student, second subject). marks[1][0] = 60 (second student, first subject). marks[1][2] = 85 (second student, third subject).
If you got 60 and 80 swapped, you read column-first, the single most common 2D slip. Ticket rule: row, THEN seat.
Quiz
How many int boxes does int marks[4][5] reserve, and which is the LAST valid element?
- 20 boxes; the last is marks[3][4]
- 20 boxes; the last is marks[4][5]
- 9 boxes; the last is marks[3][4]
- 20 boxes; the last is marks[4][4]
Show the answer
20 boxes; the last is marks[3][4]
Total = rows × columns = 4 × 5 = 20. Both indexes start at zero, so they END at rows-1 and cols-1: marks[3][4]. Writing marks[4][5] as an element commits the out-of-bounds sin in two dimensions at once. Size multiplies; last index subtracts one from each, two rules, one question.
Theory
A grid of characters: a list of names
Rows can hold text too:
char names[3][10] = {"Riya", "Aman", "Zoya"};
Each ROW is one char array: up to 9 letters plus its own '\0'. So names[1] is the whole string "Aman", and names[1][0] is the letter 'A'.
One index picks the string, two indexes pick the letter, the marks program can finally store WHO scored what, names and numbers side by side.
Watch out
Where marks leak
Column-first reading: marks[i][j] is row i, column j, always; swap them and every answer transposes. Comma indexing: marks[1, 2] compiles in C but does NOT mean row 1 column 2 (it quietly means marks[2], a genuine trap); always write marks[1][2]. Bounds in both dimensions: rows-1 and cols-1 are the ends. And each row of a char 2D array needs its own '\0' space.
Theory
BCA102 pays its dividend
You already know this grid's mathematics: order m×n, aᵢⱼ addressing, the matrix lessons of BCA102. Next lesson turns that maths into C: adding two marks grids, transposing, even matrix multiplication, all nested loops over 2D arrays. Your two subjects just merged into one skill.
Summary
Key takeaways
- int marks[3][4]: 3 rows × 4 columns = 12 boxes; rows first, columns second.
- Both indexes start at zero; the last element is [rows-1][cols-1].
- Initialize with braces inside braces, one inner brace per row.
- Walk grids with nested loops: outer i = rows, inner j = columns.
- char names[3][10]: three strings, each row with its own '\0'.
- Never index with a comma: marks[1][2], not marks[1, 2].
- Memory hook: the cinema ticket, row first, then seat.