C++20 sections · 1024 units
Open in Course

2D Array Syntax

Declaring and initializing

Access 2D array elements with two bracket pairs. Write grid[row][col] for a specific cell. First index selects the row, second selects the column within that row. Both start at 0. For int grid[3][4], valid rows are 0, 1, 2 and valid columns are 0, 1, 2, 3.

Element grid[2][3] is in third row, fourth column. Going beyond these bounds causes undefined behavior. You can read or write using this syntax. int val = grid[1][2]; reads from row 1, column 2.

grid[1][2] = 100; writes to that cell. Double-bracket notation works consistently.