MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 7 of 8

Block 1 · Unit 2Arrays, Pointers and Structures14 min · Section 7 of 8

2.7Row-Major and Column-Major Representation

Covers IGNOU MCS-208 §2.5, 2.5.1, 2.5.2

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

For the 3×43 \times 4 array

A 3 x 4 array
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)

Address calculation

Let the array have mm rows and nn columns, indices starting at 0, base address BB (address of the first element) and element size ww bytes.

In row-major order, i⋅ni \cdot n skips ii complete rows of nn elements; in column-major order, j⋅mj \cdot m skips jj complete columns of mm elements.

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

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); column-major: B+w(j⋅m+i)B + w(j \cdot 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]?

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?

Show answer

Answer: C. 2260

2000+4×(3⋅20+5)=2000+4×65=22602000 + 4 \times (3 \cdot 20 + 5) = 2000 + 4 \times 65 = 2260. 2212 is the column-major answer, 2000+4×(5⋅10+3)2000 + 4 \times (5 \cdot 10 + 3).

Question 3 · multiple choice

For the same array, what is the address of A[3][5] in column-major order?

Show answer

Answer: A. 2212

Skip 5 columns of 10 elements and then 3: 5⋅10+3=535 \cdot 10 + 3 = 53 elements, 2000+4×53=22122000 + 4 \times 53 = 2212. 2053 forgets to multiply by the element size.

Question 4 · multiple choice

What is the column-major order of the 2×32 \times 3 matrix with rows p q r and s t u?

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
#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;
}
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=8m = 8, n=6n = 6, L1=L2=1L_1 = L_2 = 1, w=4w = 4, B=500B = 500. Row-major: (5−1)⋅6+(4−1)=24+3=27(5 - 1) \cdot 6 + (4 - 1) = 24 + 3 = 27 elements, address =500+4×27=608= 500 + 4 \times 27 = 608. Column-major: (4−1)⋅8+(5−1)=24+4=28(4 - 1) \cdot 8 + (5 - 1) = 24 + 4 = 28 elements, address =500+4×28=612= 500 + 4 \times 28 = 612.

↑ ↓ move↵ openTab switch type