hunguptodry at April 22nd, 2010 23:32 — #1
Say, I have several 2D arrays, all with the same dimensions
I want to create a pointer (or some kind of alias) mapX,
and use it like so
//some magic to set mapX to point to mapa here
mapX = 1; // equiv to mapa = 1
//some magic to set mapX to point to mapb here
mapX = 1; // equiv to mapb = 1
//some magic to set mapX to point to mapc here
mapX = 1; // equiv to mapc = 1
sol_hsa at April 23rd, 2010 00:36 — #2
Step one: ditch the 2d arrays. You can do the (y*width+x) calculation yourself easily enough. This also simplifies things a lot.
If you're not able or willing to do so, however, realize that 2d arrays are nothing magical, and the compiler actually has a 1d array and is doing the above calculation for you every time you're using a 2d array.
To actually answer your question, you can make a typedef of the 2d array and then use that as the pointer type.
typedef int maptype;
hunguptodry at April 23rd, 2010 01:23 — #3
too much code to change, so sticking with the 2D arrays.
the typedef worked great. thanks.
almos at April 26th, 2010 03:57 — #4
One suggestion, if I may - perhaps not too smart, but still...
#define X_DIMENSION 512
#define Y_DIMENSION 512
typedef int maptype[X_DIMENSION][Y_DIMENSION];
reedbeta at April 26th, 2010 12:15 — #5
Almos, it would be better to do something like:
static const int xDim = 512;
static const int yDim = 512;
typedef int maptype[xDim][yDim];
oisyn at April 26th, 2010 16:55 — #6
For completeness sake, without the typedef it would've been:
int (*mapX) = mapa;
mapX = 3;
The idea is that, just like with an array of ints where you'd point to the first element in the array, now you have to point to the first element in an array of [array of int]. And each such element is an array of 512 ints, in other words, int.