gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Pointing to a double array.

#811 - Lord Graga - Fri Jan 10, 2003 8:06 pm

I have this functions:
Code:
void CheckCollision (u8* map);

I want to point this array to it:
Code:
u8 map1[10][15]; //there are data in it, it just haven't got anything to this, so i let it out to make a smaller post

But it gives me a error, what is wrong? I use ARM SDT 2250 Evaluation.

#813 - Vortex - Fri Jan 10, 2003 8:28 pm

A very simple way is to convert your array definition to
Code:

u8 map[10*15];


and just use a regular pointer. Passing two-dimentional arrays in C is pain.
One possible solution is:
Code:

void foo( int array[rows][cols] )

but you need to declare the array dimension in the function's prototype too, which IMHO sucks.

Just my $0.02

#816 - Nessie - Fri Jan 10, 2003 8:38 pm

Is there some reason you can just do this?
Code:

CheckCollision( (u8 *)&map1[0][0]);

It's admittedly a touch ugly, but ...:)

#872 - I.C.E - Sat Jan 11, 2003 7:16 pm

This two should work I think:
Code:

CheckCollision(&map1[0][0]);
CheckCollision(*map1);

#921 - DekuTree64 - Sun Jan 12, 2003 7:50 am

To pass a double-pointer just use u8 **map. Then you can access it like map[x][y] or whatever. Just picture the u8**, which points to an array of u8*s, each of which points to an array of u8s.
And if that's not confusing enough, I've actually had to use up to a u8**** before^_^

#1271 - MHz - Thu Jan 16, 2003 2:52 am

The actual reason why it doesn't work is that the compiler can't relate 'U8 *' to 'U8[10][10]'. One way you could declare your CheckCollision method is ...
Code:
void CheckCollision(U8 map[10][10]);

or ...
Code:
void CheckCollision(U8 map[][10]);

This because the compiler must know the number of "columns" in the array in order to calc the right address when indexing (if you're using an array with three dimensions it must be declared map[10][10][10] or map[][10][10] and so on). It is also important to remember, by using one of these declarations you're still passing a *pointer* to the function and NOT the whole array (i.e. call by value)!
But if you do want the method to accept an 'U8 *' parameter for some reason, you could always do as some of the others have said earlier or I'll give you another option; a simple cast ...
Code:
CheckCollision((U8 *) map);



\ MHz /

#1293 - ampz - Thu Jan 16, 2003 2:11 pm

This is how it's done.
void CheckCollision(U8 **map);

#2283 - AnthC - Sat Feb 01, 2003 8:54 am

You can have pointers to to arrays as such :-
u8 (*p)[32][32]=array;
u8 (*p)[32][32]=(u8 (*)[32][32])0x4000000;
etc.
Anth