how to create a pointer to 2D array in C
Posted 23 April 2010 - 03:32 AM
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
Posted 23 April 2010 - 04:36 AM
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;
Posted 23 April 2010 - 05:23 AM
the typedef worked great. thanks.
Posted 26 April 2010 - 07:57 AM
#define X_DIMENSION 512 #define Y_DIMENSION 512 typedef int maptype[X_DIMENSION][Y_DIMENSION];
Posted 26 April 2010 - 08:55 PM
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.
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users