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)

- 9,696
- 16
- 68
- 132

- 25
- 5
-
What you want to know ? Arrays name is its address irrespective of it's dimensions. Or you want to find the number of bytes in a 3 dimensional array ? Please make clear your question. – Mazhar Nov 25 '16 at 11:23
-
It's actually about the the internal mechanism of the language , that has to do with pointers, in order to find the address for a certain element of an array. I'm talking about the rest elements of the array, not about its first element. As far as I know, it is not sth that will help me in coding, I am just asking to understand how the language really finds the address for an array's element. – Metalingus Nov 25 '16 at 11:29
-
In short, consider you make an array of integers( 4 bytes ) . The compiler will put the array at 4 byte aligned address ( i.e multiple of 4). And each new member of array will be access by adding 4 to the start pointer. If the array you make is of chars (1 byte), then it will add 1 to the start address to access others. – Mazhar Nov 25 '16 at 11:33
-
Assume you have an array with 2 dimensions a[x][y] of integers and the address of a[0][0] is 5000. If you want to find the address of a[2][1], instead of adding 4 to 5000 until you reach a[2][1], you can simply use the mathematical function i wrote above. How can i find the same mathematical function for arrays with more dimensions? – Metalingus Nov 25 '16 at 11:40
3 Answers
let dimension of array be lxbxhxg
which means array has declared using
data_type array[l][b][h][g];
if you want address of cell at array[x][y][z][a]
then it will be
array + (x*b*h*g + y*h*g + z*g + a) * sizeof(array[x][y][z][a])

- 443
- 2
- 12
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)
.

- 74
- 6
There is a finite formula for array accessors of arbitrary dimensionality, which I will explain below.
But before, you should know that formal languages usually handle multi dimensional arrays in a grammar rule by accessing the topmost array over an index multiplied by the inner array size - which, in turn, results from a recursion. This may end up in another array, accessed by an index multiplied by the inner array size, with a specific offset and so on, until the accessed element is not an array. You should have a look at (e. g.) ANTLR, if you want to understand how formal languages work.
Of course, you can always find a specific expression, if you know the count of dimensions and the size of each dimension. For example: Let a
be the address of the array, s
an array of dimension sizes, d
the number of dimensions, c
an array of indexes (the coordinates in the space described by the array) and e
the element size. The address would evaluate in pseudocode to:
a + e * sum(i := 0, d - 1, c[i] * prod(j := i + 1, d - 1, s[j]))
where i
, j
are loop variables and sum
, prod
are the big sum/product operator with corresponding lower limit in the first and upper limit in the second parameter. Please note, the empty product operator (last iteration) results to 1. I Haven't tested the above, but the principle should be clear. Sorra, but I don't seem to be able to post formulas in mathematical notation here.

- 930
- 1
- 6
- 23