Loading...

C essentialsData

Multidimensional arrays

8 min lesson

Next
  • A multidimensional array is an array whose elements are themselves arrays, which is useful for tables, grids and matrices.
  • They matter because many real things are naturally two-dimensional, like a spreadsheet, a chessboard or the pixels of an image.
  • Think of a 2D array as a grid of mailboxes with rows and columns, like a seating chart in a theater.
  • The declaration int grid[3][4]; creates 3 rows and 4 columns, for 12 int boxes in total.
  • You access an element with two indices: grid[row][column], where both start at 0.
  • The computer stores the elements in one continuous block, row by row; this is called row-major order.
  • The syntax is: type name[rows][cols] = { {r0c0, r0c1}, {r1c0, r1c1} };.
  • A realistic use: storing the grades of several students across several tests, or the pixels of a small image.
  • A surprising detail: the first index is the row and the second is the column, and mixing them up is easy because both are just numbers.
  • The example program stores a 2x3 grid, prints it as a table, and sums all its values.
Keep in mind
  • Remember the order: grid[row][column], both starting at 0.
  • Use nested loops, one for rows and one for columns, to visit every cell.
  • Choose row and column order to match how you think about the data to avoid confusion.
  • Keep 2D arrays small when learning; large grids are better on the heap later.
  • Grids and tables are everywhere. You are doing excellent work!
main.c
#include <stdio.h>

int main(void) {
    // A 2x3 grid: 2 rows, 3 columns
    int grid[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int total = 0;

    // Outer loop over the rows
    for (int row = 0; row < 2; row++) {
        // Inner loop over the columns
        for (int col = 0; col < 3; col++) {
            // Print each cell followed by a space
            printf("%d ", grid[row][col]);
            // Add the cell value to the total
            total += grid[row][col];
        }
        // End the row with a newline so it prints like a table
        printf("\n");
    }

    // Print the sum of all six cells
    printf("Total: %d\n", total);

    return 0;
}

What happens: 1 2 3 4 5 6 Total: 21

Breaking it down

Declaration

int grid[2][3]

Creates 2 rows of 3 int each, so 6 boxes in total. The first size is rows, the second is columns.

Initializer

{ {1,2,3}, {4,5,6} }

Each inner brace fills one row. The first row becomes 1 2 3 and the second becomes 4 5 6.

Accessing a cell

grid[row][col]

Reads the cell at the given row and column. Both indices start at 0, so the first cell is grid[0][0].

Nested loops

for row ... for col ...

The outer loop visits each row, and the inner loop visits each column. Together they visit every cell exactly once.

Newline per row

printf("\n");

After each row, print a newline so the output looks like a table instead of one long line.

A helpful analogy

A 2D array is like a seating chart in a theater. Rows are numbered from the front, and seats in each row are numbered from the left. To find a seat you need both numbers: the row and the seat within that row.

Nesting rules

  • 1.List the row index first and the column index second: grid[row][col].
  • 2.Valid rows are 0 to rows - 1 and valid columns are 0 to cols - 1.
  • 3.When passing a 2D array to a function, you must specify the column count in the parameter type.
  • 4.All rows of a 2D array have the same number of columns; C does not support jagged arrays in the same type.

Common mistakes

Swapping row and column

Avoid

grid[col][row] // swapped

Use instead

grid[row][col]

The first index is the row and the second is the column. Swapping them reads a different cell or goes out of bounds.

Forgetting both indices

Avoid

printf("%d", grid); // pointer

Use instead

printf("%d", grid[1][2]);

Using one index on a 2D array gives a pointer to a row, not a single value. Always use two indices to read a cell.

Wrong size in nested loops

Avoid

for (col = 0; col < 2; col++) // rows, not cols

Use instead

for (col = 0; col < 3; col++)

The inner loop limit must match the number of columns, or you skip cells or run out of bounds.

Quick tips

  • •Draw the grid on paper before writing loops; it makes the indices obvious.
  • •Use named constants for rows and columns so the loops always match.
  • •Try changing one cell like grid[1][2] = 9; and rerun to see the total change.
  • •Print a newline at the end of each row to get a table-like output.
  • •Remember the memory is still one flat block; row-major order is why the last index varies fastest.
Previous
Next