### Multi-Dimensional Arrays

Hi,

Suppose there is an array: b[4][2][7].

The address b is 100 and int values are 4 bytes each.

First question, how would you create an access function for this?

And there was a second array..hm..I guess b[2][5][2], how do you find the address?

Not entirely sure what kind of array that is...but I'm more than confident that an array can only have two values like array b[num][num]...not a third value, you would define those later.

http://www.cplusplus.com/doc/tutorial/arrays/
They can. It's a 3D array

 First question, how would you create an access function for this?

What is an access function supposed to do?

 And there was a second array..hm..I guess b[2][5][2], how do you find the address?

The address of the first element? That is &b[0][0][0]. Or you can just write b
The access function is a function that allows access to array elements.

For a one-dimensional array, I know the access function is location(a[k]) = location(a[0])+k*elementsize.

On my review, the question just says "What is the address of b[2][5][2]?" So, I don't think it's asking for the address of the first element.
Last edited on
Mmm, i THINK it is
 `loc(a[i][j][k]) = loc(a[0][0][0]) + i*size + j*size + k*size`

Size being the bytes of the type of the array

Scratch that, it's wrong

 What is the address of b[2][5][2]?

cout << &b[2][5][2];
Last edited on
Thanks for the help!
If I have correctly understood you the "access function" for b[4][2][7] (where b[4][2][7] is an element of 3D array b) can not be defined because the definition of the array is unknown. That is it is unknown what the second and the third dimensions of the array are.
Also if to assume that my suggestion is wrong and b[4][2][7] is an array definition then it can not have such element b[2][5][2]. :)

So I think you are saying something wrong.
Topic archived. No new replies allowed.