DevAcademia lesson/C++ Basics
Multidimensional Arrays
Topic progressTopic 56 of 109 - 51% complete
~9 min lesson27 learning blocks5 code examples1 checks/interactions
Lesson outline
2D Arrays (Matrices)
A 2D array is an array of arrays, often used to represent tabular data like matrices.
In standard C++, the dimensions of a built-in multidimensional array must be compile-time constants.
Accessing Elements
Visualization of grid[2][3]:
| Row | Column 0 | Column 1 | Column 2 |
|---|---|---|---|
| 0 | 1 | 9 | 3 |
| 1 | 4 | 5 | 6 |
Nested Loops for 2D Arrays
3D and Higher Dimensions
C++ supports arrays with more than two dimensions. For example, a 3D array can represent volumetric data or RGB images.
Passing Multidimensional Arrays to Functions
All but the first dimension must be known in the parameter type. Alternatives include references to arrays or passing a flat pointer with explicit row/column counts.
Memory Layout & Indexing
For a 2D array int a[R][C], the address of a[r][c] equals &a[0][0] + (r * C + c). This is why the second dimension (C) must be known for correct indexing.
Row-major layout: all columns of row 0, then all columns of row 1, and so on.
Real-World Applications
- Representing game boards (chess, tic-tac-toe)
- Image and video processing
- Scientific computing (matrices, tensors)
- Tabular data storage
- 3D graphics and voxel-based data
Was this topic clear?