Array storage: row-major and column-major
How a 2-D array is laid out in 1-D memory — and the address formulas that turn into easy marks.
Syllabus says: “Not explicitly listed — asked in the KTU model question paper (Q2)”
On this page · 10 sections
ROW-major: finish a whole ROW first (C does this). COLUMN-major: finish a whole COLUMN first (Fortran, MATLAB).
Reading a Malayalam newspaper line by line, left to right, is row-major. Reading a column of the classifieds top to bottom before moving right is column-major.
For an m × n array with base address B and element size w (0-based): row-major address(a[i][j]) = B + (i·n + j)·w; column-major address = B + (j·m + i)·w.
What you will be able to do
- Explain row-major and column-major layout (model paper Q2).
- Compute the address of any element in either layout.
Address formulas
Row-major: Loc(a[i][j]) = B + (i × n + j) × w
Column-major: Loc(a[i][j]) = B + (j × m + i) × w
Here m = number of rows, n = number of columns, w = bytes per element, B = base address, indices start at 0. Why it works: in row-major, to reach row i you skip i full rows of n elements, then j more elements.
Row-major: B + ((i − L₁) × n + (j − L₂)) × w. Column-major: B + ((j − L₂) × m + (i − L₁)) × w.
Same array, two layouts
| col 0 | col 1 | col 2 | |
|---|---|---|---|
| row 0 | A | B | C |
| row 1 | D | E | F |
Why it matters
Memory is read in chunks (cache lines). Looping over a C array row by row touches neighbouring addresses and runs much faster than looping column by column over a large matrix.
Worked example
int a[3][4]; B = 1000, w = 4 bytes. Find a[1][2] and a[2][3].
| Element | Row-major | Column-major |
|---|---|---|
| a[1][2] | 1000 + (1·4 + 2)·4 = 1024 | 1000 + (2·3 + 1)·4 = 1028 |
| a[2][3] | 1000 + (2·4 + 3)·4 = 1044 | 1000 + (3·3 + 2)·4 = 1044 |
Asked in KTU exams (1)
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Row-major (C): B + (i·n + j)·w.
- Column-major (Fortran): B + (j·m + i)·w.
- Subtract lower bounds if indices do not start at 0.
- The last element has the same address in both layouts.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §2.6 (Representation of multidimensional arrays)
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)