Dr. Mohsin Dar
Assistant Professor
SOCS | UPES
1 / 10

Array Storage Orders

Row Major Order vs Column Major Order
C Programming - BTech First Semester
2 / 10

1-D Array Addressing

Address Calculation Formula

For a 1-D array arr[n], the address of element arr[i] is calculated as:

Address = Base Address + (i × size_of_element)

Where:

  • Base Address: Starting address of the array
  • i: Index of the element
  • size_of_element: Size of each element in bytes (e.g., 4 bytes for int)
3 / 10

1-D Array: Example

Consider: int arr[5] = {10, 20, 30, 40, 50}; starting at address 1000

arr[0]
1000-1003
arr[1]
1004-1007
arr[2]
1008-1011
arr[3]
1012-1015
arr[4]
1016-1019

Example: Address of arr[2]

Base = 1000, i = 2, size_of(int) = 4 bytes

Address = 1000 + (2 × 4) = 1008

4 / 10

What are Multi-dimensional Arrays?

a[0][0]
a[0][1]
a[0][2]
a[1][0]
a[1][1]
a[1][2]
a[2][0]
a[2][1]
a[2][2]
A 3×3 array in C appears as a 2D structure, but memory is linear!
5 / 12

The Memory Reality

Computer memory is linear (1D)

1000
1004
1008
1012
1016
1020
1024
1028
1032
How do we map 2D array indices to 1D memory addresses?
6 / 12

Row Major Order

Store elements row by row

1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
7 / 12

Column Major Order

Store elements column by column

1
2
3
4
5
6
7
8
9
1
4
7
2
5
8
3
6
9
8 / 12

Address Calculation Formulas

Row Major Order

Address = B + W × ((I - LR) × N + (J - LC))
B = Base address
W = Element size in bytes
I = Row index, J = Column index
LR = Lower row bound (0 if not specified)
LC = Lower column bound (0 if not specified)
N = Number of columns (Upper - Lower + 1)

Column Major Order

Address = B + W × ((J - LC) × M + (I - LR))
B = Base address
W = Element size in bytes
I = Row index, J = Column index
LR = Lower row bound (0 if not specified)
LC = Lower column bound (0 if not specified)
M = Number of rows (Upper - Lower + 1)
Let's solve numerical examples step by step!
9 / 12

Row Major Order - Numerical Example

Given: int arr[4][3] starting at address 1000

arr[0][0]
arr[0][1]
arr[0][2]
arr[1][0]
arr[1][1]
arr[1][2]
arr[2][0]
arr[2][1]
arr[2][2]
arr[3][0]
arr[3][1]
arr[3][2]
Find: Address of arr[2][1]
Given: Base = 1000, sizeof(int) = 4 bytes
10 / 12

Row Major - Step by Step Calculation

Address = B + W × ((I - LR) × N + (J - LC))

Step 1: Identify Values

B = 1000 (Base address) W = 4 bytes (sizeof(int)) I = 2 (row index) J = 1 (column index) LR = 0 (default lower row bound) LC = 0 (default lower column bound) N = 3 (number of columns, upper-lower+1 = 3-0+0=3)

Step 2: Apply Formula

Address = 1000 + 4 × ((2 - 0) × 3 + (1 - 0)) = 1000 + 4 × (2 × 3 + 1) = 1000 + 4 × (6 + 1) = 1000 + 4 × 7 = 1000 + 28 = 1028
Answer: arr[2][1] is stored at address 1028
11 / 12

Column Major Order - Numerical Example

Same array: int arr[4][3] starting at address 1000

arr[0][0]
arr[0][1]
arr[0][2]
arr[1][0]
arr[1][1]
arr[1][2]
arr[2][0]
arr[2][1]
arr[2][2]
arr[3][0]
arr[3][1]
arr[3][2]
Find: Address of arr[2][1] using Column Major
Note: Same element, different storage method!
12 / 12

Column Major - Step by Step Calculation

Address = B + W × ((J - LC) × M + (I - LR))

Step 1: Identify Values

B = 1000 (Base address) W = 4 bytes (sizeof(int)) I = 2 (row index) J = 1 (column index) LR = 0 (default lower row bound) LC = 0 (default lower column bound) M = 4 (number of rows, upper-lower+1 = 3-0+1=4)

Step 2: Apply Formula

Address = 1000 + 4 × ((1 - 0) × 4 + (2 - 0)) = 1000 + 4 × (1 × 4 + 2) = 1000 + 4 × (4 + 2) = 1000 + 4 × 6 = 1000 + 24 = 1024
Answer: arr[2][1] is stored at address 1024
11 / 11

C Programming Uses Row Major

int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Memory layout: 1 2 3 4 5 6 7 8 9 // arr[1][2] = Base + (1×3 + 2)×4 = Base + 20 bytes

💡 Cache Performance

Accessing elements in storage order is faster

⚡ Loop Optimization

// Good for C (Row Major) for(i = 0; i < rows; i++) for(j = 0; j < cols; j++) process(arr[i][j]);
Key Point: C always uses Row Major Order!
12 / 12

Practice Problems

Problem 1: 1-D Array

Given: int arr[10]; // Base address = 2000

Find the address of arr[7] (Assume sizeof(int) = 4).

Problem 2: 2D Array - Row Major

Given: int arr[4][5]; // Base = 3000, LR=0, LC=0

Find the address of arr[2][3] using row-major order.

Problem 3: 2D Array - Column Major

Given: float arr[3][6]; // Base = 5000, LR=0, LC=0

Find the address of arr[1][4] using column-major order (assume sizeof(float) = 4).

Problem 4: Non-Zero Lower Bounds

Given: int arr[1..4][2..5]; // Base = 1000, LR=1, LC=2

Find the address of arr[3][4] in row-major order.