class stuff
{
public:
stuff(int x, int y)
{
grid = new short int[3][x][y];
}
protected:
short int* grid;
};
int main(void)
{
stuff foo(3, 5);
return 0;
}
plz help
mattd
The world is your message board - (edited to be politically correct)
array question (c++)
uh............. how do we use ''new'' to make a multidimensional
array?
i thought it was something like this..
Offhand, I dont recall. Nonetheless try this:
xs = (x size of array)
ys = (y size of array)
int (*grid)[3][];
grid = new int [3][xs*ys];
and then reference it as such:
grid[0][y*xs+x]
regards,
GeniX
xs = (x size of array)
ys = (y size of array)
int (*grid)[3][];
grid = new int [3][xs*ys];
and then reference it as such:
grid[0][y*xs+x]
regards,
GeniX
regards,GeniXwww.cryo-genix.net
How silly of me - im sure you figured it out, but:
you could also
grid[1][y*xs+x]
or
grid[2][y*xs+x]
..
the reason you probably cant do grid[0][x][y] is because the compiler wont know what the x size of the grid is if you use a new to allocate it.
anyone else who can shed more light is more than welcome as my knowledge of such things gets iffy when u have pointers to multi-d arrays (esp. pointers to multi-d arrays of pointers
regards,
GeniX
you could also
grid[1][y*xs+x]
or
grid[2][y*xs+x]
..
the reason you probably cant do grid[0][x][y] is because the compiler wont know what the x size of the grid is if you use a new to allocate it.
anyone else who can shed more light is more than welcome as my knowledge of such things gets iffy when u have pointers to multi-d arrays (esp. pointers to multi-d arrays of pointers
regards,
GeniX
regards,GeniXwww.cryo-genix.net
July 10, 2000 11:16 PM
You can''t do it that way in C++. You can in Java, I believe.
To do it in C++, you have to do something like this:
Don''t forget to include a destructor (you''ll probably want to store x and y in stuff as well).
I think that''s how you do it... I''ve only ever done it for 2D arrays, though.
To do it in C++, you have to do something like this:
class stuff{ public: stuff (int x, int y) { grid = new short**[3]; for (int i = 0; i < 3; i++) { grid<i> = new short*[x]; for (int j = 0; j < x; j++) { grid[i][j] = new short[y]; } } } private: short ***grid;};
Don''t forget to include a destructor (you''ll probably want to store x and y in stuff as well).
I think that''s how you do it... I''ve only ever done it for 2D arrays, though.
Hey, how about this - it compiles under VC++6.0
When creating the pointer to the array the computer can only *not* know the first dimension (ie: it doesnt have to know how many repetitions of the remaining dimensions there are to access it).
So we create a pointer to such an array, initialise it (with some hax0r conversion) and can then access it as such:
int (*grid)[][3];
grid = (int(*)[][3]) new int[5][3];
(*grid)[0][0] = 00;
(*grid)[1][0] = 01;
(*grid)[5][5] = 55;
delete( grid );
Makes sense to me. Compiles and runs with no errors. So im *assuming* there is no problem with it.
regards,
GeniX
When creating the pointer to the array the computer can only *not* know the first dimension (ie: it doesnt have to know how many repetitions of the remaining dimensions there are to access it).
So we create a pointer to such an array, initialise it (with some hax0r conversion) and can then access it as such:
int (*grid)[][3];
grid = (int(*)[][3]) new int[5][3];
(*grid)[0][0] = 00;
(*grid)[1][0] = 01;
(*grid)[5][5] = 55;
delete( grid );
Makes sense to me. Compiles and runs with no errors. So im *assuming* there is no problem with it.
regards,
GeniX
regards,GeniXwww.cryo-genix.net
MAN where are my brains today... not with me im sure.
in my code you will spot a reference to 5,5 ... this doesnt exist in the array i created. Who knows why it didnt GPF under Win2k....
regards,
GeniX
in my code you will spot a reference to 5,5 ... this doesnt exist in the array i created. Who knows why it didnt GPF under Win2k....
regards,
GeniX
regards,GeniXwww.cryo-genix.net
int xs;int ys;xs = whateverYouWant; // can be entered by userys = whateverYouWant2; // can be entered by userint** array;array = new int[xs];for (int i = 0; i < xs; i++){ *array<i> = new int[ys];}
now just access it like you would a normal array
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement