Memory is one long line of bytes, but a matrix has rows and columns. So the elements of a 2-D array must be put into a single line in some order. There are two standard orders, and from either one we can calculate the address of any element directly.
The two orders
Intuition
Reading a page of a book, you read the first line left to right, then the second line, and so on: that is row-major order. Reading a newspaper column by column, top to bottom, then moving to the next column: that is column-major order.
Definition 2.15 · Row-major and column-major order
In row-major order , the elements of row 0 are stored first (in column order), then the elements of row 1, and so on. In column-major order , the elements of column 0 are stored first (in row order), then column 1, and so on.
For the 3 × 4 3 \times 4 3 × 4 array
A 3 x 4 array Copy a b c d
e f g h
i j k l
row-major order: a b c d e f g h i j k l
column-major order: a e i b f j c g k d h l (column-major is row-major order of the transpose)
Correction · Who decides the storage order
Study material says: §2.5 says that whether array elements are stored in sequence, and whether they are stored row-wise or column-wise, “depends on the operating system”.
Correct version: The programming language fixes the layout, not the operating system. An array always occupies one contiguous block. C, C++, Java (arrays of arrays), Pascal and Python’s NumPy (by default) use row-major order; Fortran, MATLAB and R use column-major order.
Why: C17 standard §6.2.5 paragraph 20 says an array type describes a “contiguously allocated” set of objects, and §6.5.2.1 paragraph 3 states that “arrays are stored in row-major order (last subscript varies fastest)”. The same C program gives the same layout on Linux, Windows or macOS.
Correction · Last element of the column-major example
Study material says: §2.5.2 gives the column-major order of the array a…l as {a, e, i, b, f, j, c, g, k, d, h, i}.
Correct version: {a, e, i, b, f, j, c, g, k, d, h, l }. The last column is d, h, l.
Why: The letter i is already the third element (column 0); each of the 12 elements appears exactly once. The program below prints the column-major order.
Address calculation
Let the array have m m m rows and n n n columns, indices starting at 0, base address B B B (address of the first element) and element size w w w bytes.
Theorem 2.16 · Address of A[i][j]
Row-major: LOC ( A [ i ] [ j ] ) = B + w ( i ⋅ n + j ) \text{LOC}(A[i][j]) = B + w\,(i \cdot n + j) LOC ( A [ i ] [ j ]) = B + w ( i ⋅ n + j )
Column-major: LOC ( A [ i ] [ j ] ) = B + w ( j ⋅ m + i ) \text{LOC}(A[i][j]) = B + w\,(j \cdot m + i) LOC ( A [ i ] [ j ]) = B + w ( j ⋅ m + i )
If the lower bounds are L 1 L_1 L 1 (rows) and L 2 L_2 L 2 (columns) instead of 0, replace i i i by i − L 1 i - L_1 i − L 1 and j j j by j − L 2 j - L_2 j − L 2 :
Row-major: LOC ( A [ i ] [ j ] ) = B + w [ ( i − L 1 ) ⋅ n + ( j − L 2 ) ] \text{LOC}(A[i][j]) = B + w\,[(i - L_1) \cdot n + (j - L_2)] LOC ( A [ i ] [ j ]) = B + w [( i − L 1 ) ⋅ n + ( j − L 2 )]
Column-major: LOC ( A [ i ] [ j ] ) = B + w [ ( j − L 2 ) ⋅ m + ( i − L 1 ) ] \text{LOC}(A[i][j]) = B + w\,[(j - L_2) \cdot m + (i - L_1)] LOC ( A [ i ] [ j ]) = B + w [( j − L 2 ) ⋅ m + ( i − L 1 )]
In row-major order, i ⋅ n i \cdot n i ⋅ n skips i i i complete rows of n n n elements; in column-major order, j ⋅ m j \cdot m j ⋅ m skips j j j complete columns of m m m elements.
Example 2.17 Address of A[2][3] in both orders
Goal. int A[5][4] (m = 5 m = 5 m = 5 rows, n = 4 n = 4 n = 4 columns, indices from 0) starts at address 1000 and each int takes 4 bytes. Find the address of A[2][3].
Row-major. Skip 2 rows of 4 elements, then 3 elements: 2 ⋅ 4 + 3 = 11 2 \cdot 4 + 3 = 11 2 ⋅ 4 + 3 = 11 elements. Address = 1000 + 4 × 11 = 1044 = 1000 + 4 \times 11 = 1044 = 1000 + 4 × 11 = 1044 .
Column-major. Skip 3 columns of 5 elements, then 2 elements: 3 ⋅ 5 + 2 = 17 3 \cdot 5 + 2 = 17 3 ⋅ 5 + 2 = 17 elements. Address = 1000 + 4 × 17 = 1068 = 1000 + 4 \times 17 = 1068 = 1000 + 4 × 17 = 1068 .
Result. A[2][3] is at 1044 in row-major order and at 1068 in column-major order.
Example 2.18 Address with lower bounds starting at 1
Goal. An array A[1..10][1..15] (10 rows, 15 columns) of 2-byte elements has base address 100. Find the address of A[4][5].
Row-major. ( i − L 1 ) ⋅ n + ( j − L 2 ) = ( 4 − 1 ) ⋅ 15 + ( 5 − 1 ) = 45 + 4 = 49 (i - L_1) \cdot n + (j - L_2) = (4 - 1) \cdot 15 + (5 - 1) = 45 + 4 = 49 ( i − L 1 ) ⋅ n + ( j − L 2 ) = ( 4 − 1 ) ⋅ 15 + ( 5 − 1 ) = 45 + 4 = 49 . Address = 100 + 2 × 49 = 198 = 100 + 2 \times 49 = 198 = 100 + 2 × 49 = 198 .
Column-major. ( j − L 2 ) ⋅ m + ( i − L 1 ) = ( 5 − 1 ) ⋅ 10 + ( 4 − 1 ) = 40 + 3 = 43 (j - L_2) \cdot m + (i - L_1) = (5 - 1) \cdot 10 + (4 - 1) = 40 + 3 = 43 ( j − L 2 ) ⋅ m + ( i − L 1 ) = ( 5 − 1 ) ⋅ 10 + ( 4 − 1 ) = 40 + 3 = 43 . Address = 100 + 2 × 43 = 186 = 100 + 2 \times 43 = 186 = 100 + 2 × 43 = 186 .
Result. A[4][5] is at 198 in row-major and 186 in column-major order.
Example 2.19 See C's row-major layout
Goal. Print the elements of a 3 × 4 3 \times 4 3 × 4 char array in memory order, check the offset formula i ⋅ n + j i \cdot n + j i ⋅ n + j , and build the column-major order.
Program. Each element is one char, so the offset in bytes equals the offset in elements.
C · row_major.c Copy #include <stdio.h>
#define M 3 /* rows */
#define N 4 /* columns */
int main( void )
{
char a[M][N] = {
{ 'a' , 'b' , 'c' , 'd' },
{ 'e' , 'f' , 'g' , 'h' },
{ 'i' , 'j' , 'k' , 'l' }
};
const char * p = ( const char * )a; /* walk the bytes of the whole array */
/* C stores the array row by row: walk through memory in order */
printf( "Memory order in C: " );
for ( int k = 0 ; k < M * N; k ++ )
printf( "%c " , p[k]);
printf( " \n " );
/* Offset of a[i][j] from the start, compared with i*N + j */
printf( "a[1][2] = %c at offset %td, formula i*N + j = %d \n " ,
a[ 1 ][ 2 ], ( const char * ) & a[ 1 ][ 2 ] - p, 1 * N + 2 );
/* Column-major order would be: element (i, j) at offset j*M + i */
char col_major[M * N];
for ( int i = 0 ; i < M; i ++ )
for ( int j = 0 ; j < N; j ++ )
col_major[j * M + i] = a[i][j];
printf( "Column-major order: " );
for ( int k = 0 ; k < M * N; k ++ )
printf( "%c " , col_major[k]);
printf( " \n " );
return 0 ;
}
Run it.
Output Copy Memory order in C: a b c d e f g h i j k l
a[1][2] = g at offset 6, formula i*N + j = 6
Column-major order: a e i b f j c g k d h l
Result. C stores a b c d e f g h i j k l (row-major); a[1][2] is at offset 1 ⋅ 4 + 2 = 6 1 \cdot 4 + 2 = 6 1 ⋅ 4 + 2 = 6 ; column-major order would be a e i b f j c g k d h l.
Row-major (C, C++, Java) a b c d | e f g h | i j k l
offset of (i, j) = i·n + j
Column-major (Fortran, MATLAB, R) a e i | b f j | c g k | d h l
offset of (i, j) = j·m + i
Row-major vs column-major for a 3 × 4 array
Note
Order matters for speed: in C, a loop that goes along a row (inner loop over j) reads neighbouring memory and uses the processor cache well; looping down columns of a large array can be several times slower.
Key idea
A 2-D array is stored in one contiguous line, row by row (row-major, as in C) or column by column (column-major, as in Fortran), and the address of A [ i ] [ j ] A[i][j] A [ i ] [ j ] is B + w ( i ⋅ n + j ) B + w(i \cdot n + j) B + w ( i ⋅ n + j ) or B + w ( j ⋅ m + i ) B + w(j \cdot m + i) B + w ( j ⋅ m + i ) respectively.
TEE Exam note
Address-calculation questions are very common (2–6 marks). Write the formula first, identify B B B , w w w , m m m , n n n and the lower bounds from the question, substitute step by step and give the final address. Watch whether indices start at 0 or 1. For theory questions, draw the matrix and the single line of memory for both orders. Check Your Progress 2: an array is stored in row-major or column-major order; row-major stores row-wise, column-major column-wise.
Common mistake
Using the number of rows m m m in the row-major formula. Row-major skips whole rows, and each row has n n n elements (the number of columns): i ⋅ n + j i \cdot n + j i ⋅ n + j .
Common mistake
Forgetting to subtract the lower bound when indices start at 1. For A[1..10][1..15], element A[1][1] must give offset 0, so use ( i − 1 ) (i - 1) ( i − 1 ) and ( j − 1 ) (j - 1) ( j − 1 ) .
Key takeaways
An array always occupies one contiguous block; the language decides the order.
Row-major: rows one after another (C, C++, Java); column-major: columns one after another (Fortran, MATLAB, R).
Row-major: B + w ( i ⋅ n + j ) B + w(i \cdot n + j) B + w ( i ⋅ n + j ) ; column-major: B + w ( j ⋅ m + i ) B + w(j \cdot m + i) B + w ( j ⋅ m + i ) , with indices shifted by their lower bounds.
Column-major order is the row-major order of the transposed matrix.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice In which order does C store the elements of int a[3][4]?
A Row-major: a[0][0], a[0][1], …, a[0][3], a[1][0], … B Column-major: a[0][0], a[1][0], a[2][0], a[0][1], … C It depends on the operating system. D In no fixed order; the compiler chooses at run time.
Check answer
Show answer Answer: A. Row-major: a[0][0], a[0][1], …, a[0][3], a[1][0], …
The C standard requires row-major order (the last subscript varies fastest). The operating system has no influence on it.
Question 2 · multiple choice int A[10][20] (indices from 0) has base address 2000 and 4-byte elements. What is the address of A[3][5] in row-major order?
A 2212 B 2065 C 2260 D 2280
Check answer
Show answer Answer: C. 2260
2000 + 4 × ( 3 ⋅ 20 + 5 ) = 2000 + 4 × 65 = 2260 2000 + 4 \times (3 \cdot 20 + 5) = 2000 + 4 \times 65 = 2260 2000 + 4 × ( 3 ⋅ 20 + 5 ) = 2000 + 4 × 65 = 2260 . 2212 is the column-major answer, 2000 + 4 × ( 5 ⋅ 10 + 3 ) 2000 + 4 \times (5 \cdot 10 + 3) 2000 + 4 × ( 5 ⋅ 10 + 3 ) .
Question 3 · multiple choice For the same array, what is the address of A[3][5] in column-major order?
A 2212 B 2260 C 2053 D 2140
Check answer
Show answer Answer: A. 2212
Skip 5 columns of 10 elements and then 3: 5 ⋅ 10 + 3 = 53 5 \cdot 10 + 3 = 53 5 ⋅ 10 + 3 = 53 elements, 2000 + 4 × 53 = 2212 2000 + 4 \times 53 = 2212 2000 + 4 × 53 = 2212 . 2053 forgets to multiply by the element size.
Question 4 · multiple choice What is the column-major order of the 2 × 3 2 \times 3 2 × 3 matrix with rows p q r and s t u?
A p q r s t u B p s q t r u C p t q u r s D s t u p q r
Check answer
Show answer Answer: B. p s q t r u
Column 0 is p, s; column 1 is q, t; column 2 is r, u. “p q r s t u” is the row-major order.
Question 5 · multiple choice What does this program print?
C · q2-layout.c Copy #include <stdio.h>
int main( void )
{
int m[ 2 ][ 3 ] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }};
int * p = ( int * )m;
printf( "%d %d \n " , p[ 3 ], p[ 1 * 3 + 2 ]);
return 0 ;
} A 2 5 B 3 6 C 4 6 D 4 5
Check answer
Show answer Answer: C. 4 6
Row-major memory holds 1 2 3 4 5 6, so p[3] is 4 (m[1][0]) and p[5] is 6 (m[1][2]). “2 5” would be column-major order (1 4 2 5 3 6).
Question 6 · short answer
An array X[1..8][1..6] (8 rows, 6 columns) of 4-byte elements starts at address 500. Find the address of X[5][4] in row-major and in column-major order.
Show worked answer Here m = 8 m = 8 m = 8 , n = 6 n = 6 n = 6 , L 1 = L 2 = 1 L_1 = L_2 = 1 L 1 = L 2 = 1 , w = 4 w = 4 w = 4 , B = 500 B = 500 B = 500 . Row-major: ( 5 − 1 ) ⋅ 6 + ( 4 − 1 ) = 24 + 3 = 27 (5 - 1) \cdot 6 + (4 - 1) = 24 + 3 = 27 ( 5 − 1 ) ⋅ 6 + ( 4 − 1 ) = 24 + 3 = 27 elements, address = 500 + 4 × 27 = 608 = 500 + 4 \times 27 = 608 = 500 + 4 × 27 = 608 . Column-major: ( 4 − 1 ) ⋅ 8 + ( 5 − 1 ) = 24 + 4 = 28 (4 - 1) \cdot 8 + (5 - 1) = 24 + 4 = 28 ( 4 − 1 ) ⋅ 8 + ( 5 − 1 ) = 24 + 4 = 28 elements, address = 500 + 4 × 28 = 612 = 500 + 4 \times 28 = 612 = 500 + 4 × 28 = 612 .