Sunday, May 1, 2011

Three Dimensional Arrays

How do you access a three dimensional array? It's on my exam tomorrow, I'll try to explain it and in the process maybe I'll understand it better myself.

Say you have initialized a three dimensional array:
int array[x][y][z]
and you want to access:
array[A][B][C]

the offset will therefore = zyA + zB + C. Add that to the array's base address and you will have the address of the element at array[A][B][C]

Similarly for two dimensions
int Array[x][y]

offset = yA + B

I'll try to add illustrations after my exams, but it makes sense when you draw out out the memory array itself.