Two-Dimensional Arrays in C: Concepts, Declaration, and Initialization

A one-dimensional array (covered in Post 3) is a single row of values. A two-dimensional array extends this into a grid — rows and columns — perfect for representing matrices, tables, and grids. This is Post 4 of the Unit II series.

Declaration and Initialization

int matrix[3][4];   // 3 rows, 4 columns — 12 ints total, uninitialized

int grid[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

int flat[2][3] = {1, 2, 3, 4, 5, 6};   // same values, without the nested braces
How to read the declaration: int matrix[3][4] means: 3 rows, each row itself being an array of 4 ints. Think of it as “an array of arrays” — matrix[0] is the entire first row (an array of 4 ints), and matrix[0][2] is the 3rd element within that first row.

Accessing Elements: Row and Column Index

#include <stdio.h>

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

    printf("Element at row 0, col 2: %d\n", grid[0][2]);
    printf("Element at row 1, col 0: %d\n", grid[1][0]);

    grid[1][1] = 99;   // modify
    printf("Updated element at row 1, col 1: %d\n", grid[1][1]);

    return 0;
}

Sample Output

Element at row 0, col 2: 3
Element at row 1, col 0: 4
Updated element at row 1, col 1: 99

Reading and Printing a Full Matrix

#include <stdio.h>

int main() {
    int rows, cols;
    int matrix[10][10];

    printf("Enter rows and columns: ");
    scanf("%d %d", &rows, &cols);

    printf("Enter %d elements:\n", rows * cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("Matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Sample Output

Enter rows and columns: 2 3
Enter 6 elements:
1 2 3
4 5 6
Matrix:
1 2 3
4 5 6
The nested loop pattern: Reading/printing a 2D array almost always uses TWO nested loops: the outer loop walks through rows, the inner loop walks through columns of the current row. This pattern shows up constantly — in matrix addition, transpose, searching, and more.

Row-Major Order: How 2D Arrays Are Actually Stored

Memory itself is one-dimensional (a single sequence of addresses) — there’s no such thing as truly “2D” memory. C stores a 2D array in row-major order: the entire first row, then the entire second row, and so on, laid out back-to-back.

int grid[2][3] = {{1,2,3},{4,5,6}};

Memory layout: [1][2][3][4][5][6]
                row 0    row 1
Related reading on this site: This blog already has worked, detailed examples for adding two 2D matrices, finding a matrix’s transpose, searching for an element in a 2D array, and checking whether a matrix is symmetric — all built directly on the concepts in this post.

Summary

  • A 2D array is declared as type name[rows][cols] and accessed as name[row][col].
  • Think of it as an array of arrays — each row is itself a 1D array.
  • Processing a 2D array almost always uses nested loops: outer for rows, inner for columns.
  • Internally, C stores 2D arrays in row-major order — one row after another in a single contiguous block of memory.

Further Reading

Next in this series: character arrays and string handling functions, closing out Unit II.

Leave a Comment