I'm making a game and I need a 2-D map allocated on the heap... something like this..
but when i try to compile, i get an error that says cannot assign from int[*][2]Code:int * pInt = new int[2][2];
any ideas?
I'm making a game and I need a 2-D map allocated on the heap... something like this..
but when i try to compile, i get an error that says cannot assign from int[*][2]Code:int * pInt = new int[2][2];
any ideas?
AOL: GrNxxDaY
IDE: Dev-C++ Beta 5 (v4.9.4.1)
Project: Eye of Sahjz (text-RPG)
If you think I may need help, please IM me.
A double dimensioned array isn't your normal pointer'd beast. It's viewed a little differently in memory, than other things.
What you can do is allocate your memory and then access it via pointers directly. 2-D is really just x,y dimensions (or height/width)...
It is not the spoon that bends, it is you who bends around the spoon.
Sayeh is correct, but if you still wish to use multi-dimensional arrays,
you must allocate the memory for the (pointers/arrays) individially.
Code:#define PINT_SIZE1 2 #define PINT_SIZE2 2 int * pInt = new int[PINT_SIZE1]; for(int i = 0; i < PINT_SIZE1;i++) pInt[i] = new int[PINT_SIZE2];
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
sayeh... can you tell me if i am understanding this right..
pInt is a pointer to an array of 2 ints on the heap, but you dont store ints in them, you store pointers to ints..?
AOL: GrNxxDaY
IDE: Dev-C++ Beta 5 (v4.9.4.1)
Project: Eye of Sahjz (text-RPG)
If you think I may need help, please IM me.
Sorry for the delay in response...
In any array with more than 1 dimension, the first element is a pointer. If it's a single-dimensioned array, then of course it's actual data.
it is _much_ easier to keep a pointer for the base address of your 2D map, and calculate the address of the data you want to access.
For example, map size = 10 x 10. So, in code:
You have a base address (map), a scratchpad address (temp), and your row/column indexes (x,y).Code:#define WIDE 10 #define HIGH 10 int *map; int *temp; int data; int x,y; map = 0L; /* init vars to _known_ state */ temp = 0L; map = (int*)malloc(sizeof(int) * (WIDE * HIGH)); /* allocate RAM for map */ if(map) /* allocate okay? */ { x = 5; y = 9; temp = (map + ((long)x * (long)y)); /* calculate address of position x,y in map */ data = *temp; free(map); /* deallocate map buffer */ map = 0L; }; . . .
It is not the spoon that bends, it is you who bends around the spoon.