Load_Map(tile_t tiles[][ROWS_PER_ROOM][COLS_PER_ROOM])
{
// Load tiles into m_tiles[][][];
}
Load_Room(DWORD roomID)
{
Init_Room(m_tiles[roomID]);
}
Init_Room(tile_t tiles[][COLS_PER_ROOM])
{
// Load tiles into m_tiles[][];
}
Thanks for your time.
C++: 3 Dim array - passing as a 2 Dim array.
I couldn't find any articles/tutorials dealing with this, so here goes:
Basically in my Map class I have the tiles broken down by room id, then row and col per room. This creates a 3 dim array. In the room class, I just have the 2 dim array of rows and cols. I'm looking to pass only the 2 dim portion of the Maps 3 dim array into the Room class. Hope that makes sense. :
all you need todo is pass the tiles array with the first index filled in:
Init_Room(tiles[roomID]);
Then its only passin your a double pointer which is what your after.
Init_Room(tiles[roomID]);
Then its only passin your a double pointer which is what your after.
Me again. :) More array fun!
Okay here's what I got, and it's giving me an error (commented below).
Where'd I goof this one up at? :) Basically I'm looking to snag a pointer to the 2 dim array and still use the data as a 2 dim array, rather than copying the data to a new array.
Okay here's what I got, and it's giving me an error (commented below).
tile_t* m_pTiles;void Func1(tile_t tiles[][COL_LENGTH]){ m_pTiles = &tiles[0][0];}const tile_t& GetTile(DWORD r, DWORD c){ return m_pTiles[r][c]; // Error on this line!}
Where'd I goof this one up at? :) Basically I'm looking to snag a pointer to the 2 dim array and still use the data as a 2 dim array, rather than copying the data to a new array.
Just realized that the error may help:
error C2676: binary '[' : 'tile_t' does not define this operator or a conversion to a type acceptable to the predefined operator
If I comment out one of the [#]'s it works just fine (basically treating it as a single array - m_pTiles[r];). But it's a pointer to a 2 dim array. :
I guess I'll do some hunting around to see how I can manually calculate the [r][c] position, although that's a bit undesirable as I'd prefer not to do calculations.
While I'm here, is it possible to do the following and have two arrays point to the same array?:
tile_t array[][length];
Func1(tile_t a[][length]) { array = a; }
?
Thanks in advance.
EDIT:
Well I found this: http://www.pa.msu.edu/courses/1996spring/PHY405/lect3e.html
It looks like he's declaring pointers to array with
T (*array)[r][c];
I'm going to give it a shot. Assuming I'm reading the syntax correctly, it will hopefully work. :)
[Edited by - wyrd on February 9, 2005 5:44:32 PM]
error C2676: binary '[' : 'tile_t' does not define this operator or a conversion to a type acceptable to the predefined operator
If I comment out one of the [#]'s it works just fine (basically treating it as a single array - m_pTiles[r];). But it's a pointer to a 2 dim array. :
I guess I'll do some hunting around to see how I can manually calculate the [r][c] position, although that's a bit undesirable as I'd prefer not to do calculations.
While I'm here, is it possible to do the following and have two arrays point to the same array?:
tile_t array[][length];
Func1(tile_t a[][length]) { array = a; }
?
Thanks in advance.
EDIT:
Well I found this: http://www.pa.msu.edu/courses/1996spring/PHY405/lect3e.html
It looks like he's declaring pointers to array with
T (*array)[r][c];
I'm going to give it a shot. Assuming I'm reading the syntax correctly, it will hopefully work. :)
[Edited by - wyrd on February 9, 2005 5:44:32 PM]
your setting the pointer m_pTiles to a DOUBLE pointer tiles. Remember that an array is just a pointer, so a double array is a double pointer. This should fix your problem:
tile_t **m_pTiles;
tile_t **m_pTiles;
Passing around multiple-dimensional arrays is difficult to get right in general - and you can't return plain arrays, at all, period. Since ROWS_PER_ROOM and COLS_PER_ROOM appear to be constant, my recommendation would be to make a struct "wrapper" for a room's cells:
Er... I think that's right... :)
struct Grid { tile_t _[ROWS_PER_ROOM][COLS_PER_ROOM];};Load_Map(Grid tiles[]) { // Load tiles into m_tiles[];}Load_Room(DWORD roomID) { Init_Room(m_tiles[roomID]);}Init_Room(Grid g) { // Load g into m_grid;}tile_t* m_pTiles;void Func1(Grid g) { m_pTiles = g._; // Although maybe you want to make the member be an actual Grid instead... // of course, that would copy the data, while this just points at it. // (Of course, you need to make sure the Grid data is persistent if you're // going to point at it!)}const tile_t& GetTile(DWORD r, DWORD c) { return m_pTiles[r][c];}
Er... I think that's right... :)
Just pass the array through the base pointer. It's easier, and (AFAIK, by the standard) foolproof.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement