Array Address Calculation
Array Address Calculation
Address computation
1D array – address calculation
• Let A be a one dimensional array. B 100
• Formula to compute the address of the
Ith element of an array (A[I]) is: [0] 1 W
Address of A[I] = B + W * ( I – LB ) [1] 2 104
where, 108
B = Base address/address of first element, i.e. A[LB].
[2] 3
W = Number of bytes used to store a single array [3] 4 112
element.
[4] 5 116
I = Subscript of element whose address is to be
found. [5] 6 120
LB = Lower limit / Lower Bound of subscript, if not
specified assume 0 (zero).
Thapar University UCS406 - Data Structures and Algorithms 2
Example
• Similarly, for a character array where a single
character uses 1 byte of storage.
• If the base address is 1200 then,
Address of A[I] = B + W * ( I – LB )
Address of A[0] = 1200 + 1 * (0 – 0) = 1200
Address of A[1] = 1200 + 1 * (1 – 0) = 1201
…
Address of A[10] = 1200 + 1 * (10 – 0) = 1210
Thapar University UCS406 - Data Structures and Algorithms 3
Example
• If LB = 5, Loc(A[LB]) = 1200, and W = 4.
• Find Loc(A[8]).
Address of A[I] = B + W * ( I – LB )
Loc(A[8]) = Loc(A[5]) + 4 * (8 – 5)
= 1200 + 4 * 3
= 1200 + 12
= 1212
• Column Major
Address of A [I][J] = B + W * (( I – LBR ) + M * ( J – LBC ))