Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Bonus · not in the official syllabusAsked 1× in KTU papersLesson live

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
ormakkurippu ✎
Never-forget card

ROW-major: finish a whole ROW first (C does this). COLUMN-major: finish a whole COLUMN first (Fortran, MATLAB).

Picture it

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.

Write this in the exam

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.

Pro tipWhen indices start at L₁ and L₂ (e.g. a[1..m][1..n])

Row-major: B + ((i − L₁) × n + (j − L₂)) × w. Column-major: B + ((j − L₂) × m + (i − L₁)) × w.

Same array, two layouts

col 0col 1col 2
row 0ABC
row 1DEF
A 2 × 3 array.
A
[0][0]
B
[0][1]
C
[0][2]
D
[1][0]
E
[1][1]
F
[1][2]
Row-major: row 0 completely, then row 1.
A
[0][0]
D
[1][0]
B
[0][1]
E
[1][1]
C
[0][2]
F
[1][2]
Column-major: column 0 completely, then column 1, then column 2.

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].

a[2][3] is the last element (index 11) in both layouts, so the addresses match — a quick self-check.
ElementRow-majorColumn-major
a[1][2]1000 + (1·4 + 2)·4 = 10241000 + (2·3 + 1)·4 = 1028
a[2][3]1000 + (2·4 + 3)·4 = 10441000 + (3·3 + 2)·4 = 1044

Asked in KTU exams (1)

Quick check — can you get them all?

Question 1

Q1.int a[3][4], base 1000, 4 bytes per int, row-major. Address of a[1][2]?

Question 2

Q2.Same array, column-major. Address of a[1][2]?

Question 3

Q3.C stores 2-D arrays in…

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)