intermediate18 min

Multi-Dimensional Arrays

Declaring and iterating over a two-dimensional array with nested loops.

What you'll learn

  • Declare and initialize a two-dimensional array
  • Access an element with row/column indexing, e.g. grid[row][col]
  • Iterate over a 2D array with nested for loops

Explanation

A two-dimensional array is declared with two size brackets: int grid[2][3]; creates a grid of 2 rows and 3 columns (6 elements total). You can initialize it with nested braces, one inner brace list per row: int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};.

You access a single element with two indices, row first: grid[row][col]. Under the hood, a C 2D array is really laid out as one contiguous block of memory, row by row (this is called row-major order) -- grid[0][2] and grid[1][0] are actually adjacent in memory, even though they look far apart when you write them out.

Processing every element of a 2D array almost always means a nested loop: an outer loop over rows, and an inner loop over columns within that row: for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { ... grid[row][col] ... } }. This pattern -- outer loop for rows, inner loop for columns -- comes up constantly whenever you're working with grids, matrices, or tables of data in C.

Guided lab

Predict: Summing a 2D array with nested loops

CNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints, including spacing.

#include <stdio.h>

int main(void) {
    int grid[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int total = 0;
    for (int row = 0; row < 2; row++) {
        for (int col = 0; col < 3; col++) {
            total += grid[row][col];
            printf("%d ", grid[row][col]);
        }
        printf("\n");
    }
    printf("Total: %d\n", total);

    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Swapping row and column in the index order, e.g. writing grid[col][row] instead of grid[row][col].
  • Forgetting that a 2D array's total element count is rows times columns, and mis-sizing a loop bound as a result.
  • Assuming a 2D array is a true 'array of arrays' pointer structure rather than one contiguous block laid out in row-major order.

Knowledge check

Knowledge check

1. How many total elements does `int grid[2][3]` hold?
2. What loop structure is typically used to visit every element of a 2D array?
3. In C's row-major layout, which two elements are adjacent in memory for `grid[2][3]`?

Takeaway

Index a 2D array as grid[row][col], and process it with a nested loop -- an outer loop over rows and an inner loop over columns.

Summary

A 2D array is declared with two size brackets and stored contiguously in row-major order; nested loops (rows outside, columns inside) are the standard way to visit every element.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.