Multidimensional Arrays in C++
Multidimensional arrays nest the same idea: int grid[3][4]; is three rows of four ints, still one flat contiguous block in row-major order. Indexing grid[r][c] walks rows then columns, and nested loops (outer rows, inner columns) are the natural traversal.
Only the first dimension may be deduced from an initializer, and passing them to functions requires all but the first dimension in the signature (void f(int m[][4])) — another reason modern code reaches for vector<vector<int>> or flat vectors with manual indexing.
Rows of rows, one block
int grid[3][4]; holds 12 ints contiguously, row after row — grid[1][0] sits right after grid[0][3] in memory.
Nested loop traversal
Outer loop over rows, inner over columns visits every cell exactly once — the grid-processing standard shape.
Partial deduction only
int g[][2]{{1,2},{3,4}}; deduces 2 rows, but the column count must always be spelled out.
Function signatures pin dimensions
A parameter int m[][4] hard-codes 4 columns — mismatched widths won't compile, unlike flat-pointer hacks.
#include <iostream> int main() { int grid[2][3]{{1, 2, 3}, {4, 5, 6}}; int sum = 0; for (int r = 0; r < 2; ++r) { for (int c = 0; c < 3; ++c) { sum += grid[r][c]; } } std::cout << sum << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.