For a 1-D array arr[n], the address of element arr[i] is calculated as:
Address = Base Address + (i × size_of_element)
Where:
Consider: int arr[5] = {10, 20, 30, 40, 50}; starting at address 1000
Example: Address of arr[2]
Base = 1000, i = 2, size_of(int) = 4 bytes
Address = 1000 + (2 × 4) = 1008
Given: int arr[10]; // Base address = 2000
Find the address of arr[7] (Assume sizeof(int) = 4).
Using formula: Address = Base + (i - LB) × W
= 2000 + (7 - 0) × 4 = 2000 + 28 = 2028
Given: int arr[4][5]; // Base = 3000, LR=0, LC=0
Find the address of arr[2][3] using row-major order.
Using formula: Address = B + W × ((I - LR) × N + (J - LC))
= 3000 + 4 × ((2-0) × 5 + (3-0)) = 3000 + 4 × (10 + 3) = 3000 + 52 = 3052
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).
Using formula: Address = B + W × ((J - LC) × M + (I - LR))
= 5000 + 4 × ((4-0) × 3 + (1-0)) = 5000 + 4 × (12 + 1) = 5000 + 52 = 5052
Given: int arr[1..4][2..5]; // Base = 1000, LR=1, LC=2
Find the address of arr[3][4] in row-major order.
N = 5-2+1 = 4 (number of columns)
Using formula: Address = 1000 + 4 × ((3-1) × 4 + (4-2))
= 1000 + 4 × (8 + 2) = 1000 + 40 = 1040