previous | start | next

Array Element Addresses

The assembler code to access an element of a one dimensional array needs to calculate the address of this element.

The compiler will know the beginning address of the array.

The calculation may depend on whether the subsript is a constant or an expression.

 int a[5];

 address(a[3]) = address(a[0]) + (number of elements that come before a[3]) * size(int)
   

But since array subscripts start at 0, the number of elements before a[3] is 3: a[0], a[1], and a[2]

So the array access formula for a[3] is

 int a[5];

 address(a[3]) = address(a[0]) + 3 * size(int) = address(a[0]) + 3 * 4
               = address(a[0]) + 12

   


previous | start | next