Procedural Programming with C++
Explainer
How C++ Stores a 2D Array in Memory
A 2D array in C++ is really one long row in memory.
Quick check · tap an answer to continue
Lesson notes
Key points
- Declaring int m[2][3]; creates two rows of three integers, accessed as m[row][column].
- In memory, C++ uses row-major order: all of row zero is stored first, then row one.
- The element m[i][j] sits at position i times the number of columns plus j from the start.
- That is why a function parameter must state the column count, like int m[][3], so it can compute offsets.
Takeaway: 2D arrays are stored row by row; columns fix the layout.
Quick check: Why must int m[][3] keep the 3 in a parameter?
- To compute element offsets
- To limit the number of rows
- To stop pass by value
2D arrayRow-major orderIndex formulaColumn size required
Lessons like this, for your syllabus.
Short narrated study videos written for your course, level and exam. Free to start.
Get lessons for your own syllabus