carraysarray-address

Array's address with dimension greater than 3


What is the mathematical function that gives the address of an array with more than 3 dimensions? I already know that for an array a[x][y] with 2 dimensions is (a (x * max_second_dimension + y) * byte)


Solution

  • Given an array a[N1][N2][N3]...[Nk] the address of the element a[i1][i2]...[ik] would be equal to:

    a +
    + i1 * (N2 * N3 * ... * Nk) +
    + i2 * (N3 * N4 * ... * Nk) +
    + i3 * (N4 * N5 * ... * Nk) +
    ...
    + i(k - 1) * Nk +
    + ik
    

    Here what follows i and N are indexes (and so is (k - 1) in i(k - 1).