Note that this has probably been answered before, but now in a way that I could understand it.
I'm trying to understand how pointers and arrays work together. I thought I understood both how and why 1D and 2D arrays could be accessed with pointers until I tried to wrap my head around 3D arrays (just to be clear, I understand 3D arrays, just not how to access them with pointers).
So let's say I have an array x[5]
and I want to access the element x[1]
using pointers. In that case, I can use *(&x[0]+1)
, because x is the same as &x[0] and *(x + 1) can be used to access a[1].
Now, for a 2D array, let's say it is declared as x[5][3]
.
To access x[1][2], I can use *(&x[0][0]+3*1+2)
. I can kind of understand this. We start at memory location x[0][0], then add 3 * 1 to get from x[0][0]
to x[1][0]
, and then add 2 to get from x[1][0]
to x[1][2]
.
The above makes sense to me until I get to a three dimensional array. Let's assume that x
is declared as x[3][4][5]
, how would I get to, let's say element x[2][1][3]
using the same logic(if possible)? Also, how can I apply this logic to higher dimensional arrays as well?
Any advice on this is greatly appreciated!