an array in an array...

Printable View

• 12-14-2002
Supar
an array in an array...
I have made several "maps" and would like an easy way to call all of them into a function.

the maps look similiar to:
char map1[2][4]={{g,g,g,g},
{g,w,w,g}};

now to call it into a function i merely did...
Code:

```int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct) { for(x=0;x<20;x++)         {                 for(int y=0;y<40; y++)                 {                         PMap[x][y];                         if(PMap[x][y]==g)                         {fgreen;}                         if(PMap[x][y]==c)                         {                                 charX=x;                                 charY=y;                                 fiblue;                         }                         if(PMap[x][y]==w)                         {grey;}                         if(PMap[x][y]==r)                         {fblue;}                         if(PMap[x][y]==d)                         {brown;}                         cout<<PMap[x][y];                 }                 cout<<endl;         } }```
so that all works good and fine...until a character walks off the edge of the map...

i need someway to call in the next map...which i could do if I could put each of the maps into an array...or something similiar...just wondering what i could do...

BTW:i cut alot out of my code...so its small enough to glance over...
• 12-14-2002
Waldo2k2
Well, instead of using another containing array to pass your existing array into the function, use a pointer. I think I missed the second question you asked so if you do need to know more let us know.
• 12-14-2002
Supar
i tried the whole pointer thing...but it is giving me the error...

C:\Program Files\Microsoft Visual Studio\MyProjects\Practice17\Practice17.cpp(59) : error C2440: '=' : cannot convert from 'char (*)[20][40]' to 'char *'

Code:

```char map1[20][40];         for(x=0;x<20;x++)         {                 for(y=0;y<40;y++)                 {                         map1[x][y]=g;                 }         } char *ptm1,*ptm2; char map2[20][40];         for(x=0;x<20;x++)         {                 for(y=0;y<40;y++)                 {                         map2[x][y]=g;                 }         } ptm1=map2; ptm2=map2;```
as for what im trying to do...

i have made a small 'character' map in console...and i have a little character that you can move around the map...now i want to make a map function that allows me to just input the map and a few other variables so i can connect the maps...

the pointer thing would have worked great...if it had worked...

i need something that i can include in a function parameter...
that i can just have inputted in depending on which map was called..so the program would know which map to go to when the character 'walked off the map'

if this is confusing...im sorry =(
• 12-14-2002
PJYelton
Not sure if I understand you correctly, but maybe you could add an extra dimension into your array. So instead it would look like this: map[map #][x pos][y pos]
• 12-14-2002
Shadow12345
int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct);

I would make that prototype be
int MAPFUNCTION(char *,int sx,int sy,int drct);

Is that what you already tried? If so that should work fine as far as I know
• 12-14-2002
Supar
gah...cant believe i didnt think of giving the array a third []...that should work great...thanks

:cool:
• 12-14-2002
Supar
hm...one problem...how do you initialize the array?

wmap[2][2][2]{{g,g},
{g,g}},
{{g,g},
{g,g}};

?
• 12-14-2002
PJYelton
I think you are missing a pair of brackets, put another one at the beginning and one at the end.
• 12-14-2002
Eibro
Quote:

Originally posted by Shadow12345
int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct);

I would make that prototype be
int MAPFUNCTION(char *,int sx,int sy,int drct);

Is that what you already tried? If so that should work fine as far as I know

No, you'll need int MAPFUNCTION(char **,int sx,int sy,int drct);
That, being the exact same as
int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct)
as one address is still pushed onto the stack
• 12-14-2002
Eibro
Re: an array in an array...
Quote:

Originally posted by Supar
I have made several "maps" and would like an easy way to call all of them into a function.

the maps look similiar to:
char map1[2][4]={{g,g,g,g},
{g,w,w,g}};

now to call it into a function i merely did...
Code:

```int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct) { for(x=0;x<20;x++)         {                 for(int y=0;y<40; y++)                 {                         PMap[x][y];                         if(PMap[x][y]==g)                         {fgreen;}                         if(PMap[x][y]==c)                         {                                 charX=x;                                 charY=y;                                 fiblue;                         }                         if(PMap[x][y]==w)                         {grey;}                         if(PMap[x][y]==r)                         {fblue;}                         if(PMap[x][y]==d)                         {brown;}                         cout<<PMap[x][y];                 }                 cout<<endl;         } }```
so that all works good and fine...until a character walks off the edge of the map...

i need someway to call in the next map...which i could do if I could put each of the maps into an array...or something similiar...just wondering what i could do...

BTW:i cut alot out of my code...so its small enough to glance over...

Instead of putting a third dimension onto your list, why not put your data into a structure or class and create a linked list? Instead of a single link, you could have four... one for each direction. Then you could easily and efficiently decide what map to load based on situations that arise.

It may be a little over your head, (I have no idea what your coding abilities are though) but I think it would be a much better design.
• 12-14-2002
Shadow12345
Quote:

Originally posted by Eibro
No, you'll need int MAPFUNCTION(char **,int sx,int sy,int drct);
That, being the exact same as
int MAPFUNCTION(char Pmap[20][40],int sx,int sy,int drct)
as one address is still pushed onto the stack

sweet I actually learned something useful just now! Eibro is my hero! *bows before Eibro*

hey Eibro aren't you dual catfish? (dual catfish++)
• 12-14-2002
Supar
ah im learning about linked lists right now...getting a little confused...tutorials dont always make things so simple to newbit programmers ;P

but ill look into doing it that way.
• 12-14-2002
Eibro
Quote:

Originally posted by Shadow12345
sweet I actually learned something useful just now! Eibro is my hero! *bows before Eibro*

hey Eibro aren't you dual catfish? (dual catfish++)
Yep, that's me!

Quote:

ah im learning about linked lists right now...getting a little confused...tutorials dont always make things so simple to newbit programmers ;P

but ill look into doing it that way.
It may not be easy, but you'll learn a lot more doing it that way than you will doing it a way which you're already familiar with.
• 12-14-2002
Supar
yea...i agree...linked lists it is then...im sure ill need help...so be on the lookout ;P