Skip to content
AgileLogics
3 min read

Multidimensional Arrays

Prerequisites: Arrays

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

printf("%d\n", grid[1][2]); // 6

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

A 2D array is laid out in memory as one single contiguous block, row after row — grid[1][2] and grid[0][5] (if it existed) would actually refer to the exact same memory address, which is why C's lack of bounds checking is especially easy to trip over here.