Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

LifeOfTamir

Accessing multidiemsional arrays passed in as pointers

This topic is 5301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey people, I''ve tried to search everywhere for a solution to this problem and i haven''t been able to find anything so i''m hoping one of you geniuses will be able to help me out I have this function: CreateGraph(int** map, int sizex, int sizey) map is a two dimensional array, the method gets called more than once with arrays of different sizex. Thats the reason why i''ve had to pass it in as int**. The problem now is that i can''t just access the array using the conventional map[0][0]. I''ve tried lots of ways to try and access the array, including just desperate guess work when all else was failing. Does anyone know how to solve this? Many thanks T

Share this post


Link to post
Share on other sites
Advertisement
If x and y are your coordinates:

int val_at_x_y = (*map)[x + xsize * y];

Make sure 0 <= x + xsize * y < (xsize * ysize) though.



Share this post


Link to post
Share on other sites
thanks for the quick reply

but i''m not sure how that code works?

If the array is 2D...surely x + xsize * y is if i just had one big 1D array acting as if it was a 2D array?

Share this post


Link to post
Share on other sites

The 2D array is converted to a 1D array by accessing the first element map[0]. In this case, it''s been dereferenced (*map), which is the same thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,

I used a structure similar to the following:


class Array
{
public:
struct elem{
T* data;
};

elem *data;

Array( int sizeX, int sizeY ){
data = new elem[sizeX];
for( int i = 0; i < sizeX; i++ )
data.data = new T[ sizeY ];
}

inline T GetData( int i, int j ){
return data[i].data[j];
}

/// etc ...

};

Share this post


Link to post
Share on other sites
hmm...i still seem to be having trouble.

i tried using parla's suggestion but i kept on getting an access violation. Onemind's suggestion was greeted with something similiar although int *mapLine = map[x] worked it was the second line which was causing problems.

when i ran in debug mode ( i wanted to check that i was actually passing in something and that it wasn't just null )

map is said to have value 0x004f8020 (good, not null and pointer address) int (* map_cost[24]) (map_cost is the array being passed in, size [40][24] )

after int *mapLine = map[x] though mapLine has value 0x0000000a, which is null i think.

That might mean i'm passing it in incorrectly

but surely if
int map_cost[y][x] = *whatever*, then to pass it in all i need to do is (int**)map_cost?

thanks again

[edited by - LifeOfTamir on June 8, 2004 12:29:55 PM]

Share this post


Link to post
Share on other sites
Arrays are zero-based.

If the array is 40x24, and you try to access element 24 in the last row, you''re out of bounds. (40x24 = 960 -- element 40,24 = 1025).

Share this post


Link to post
Share on other sites
I know arrays are zero based, this is when it''s trying to access the first element..i.e [0][0]

functino code:

CGraph* CGraph::CreateGraph(int** map, int sizex, int sizey, int tilesize)
{
CGraph* nodeSet = NULL ;

int ix, iy; //used for looping


int id = 1;

int countx = 0;
int county = 0;

int mapx = 0;
int mapy = 0;

int val_at_x_y;
int *mapLine;


//int halfTileSize = tilesize / 2;


int ypos = 0;//halfTileSize;

int xpos = 0;//halfTileSize;


for (iy = 0; iy < sizey; iy++)
{
for (ix = 0; ix < sizex; ix++)
{
mapLine = map[iy];
error is here ---> val_at_x_y = mapLine[ix];
//map[mapx][mapy] = cost; count = id

nodeSet = new CGraph(new CNode(NULL,new CNodeData(xpos,ypos,val_at_x_y),id),nodeSet);
id++;
mapx++;
countx++;

xpos = (countx*tilesize);//+halfTileSize;

}
mapy++;
mapx = 0;
countx=0;
county++;

xpos = 0;//halfTileSize;

ypos = (county*tilesize);//+halfTileSize;

}
.
.
.


this is the call
CreateGraph((int**)map_cost, WORLD_SIZEX, WORLD_SIZEY, TILE_SIZE)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!