Thread: an array in an array...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    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...
    MSVC++~

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    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 =(
    MSVC++~

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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]

  5. #5
    Shadow12345
    Guest
    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

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    gah...cant believe i didnt think of giving the array a third []...that should work great...thanks

    MSVC++~

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    hm...one problem...how do you initialize the array?


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


    ?
    MSVC++~

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I think you are missing a pair of brackets, put another one at the beginning and one at the end.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: an array in an array...

    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.

  11. #11
    Shadow12345
    Guest
    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. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    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.
    MSVC++~

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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!

    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.

  14. #14
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    yea...i agree...linked lists it is then...im sure ill need help...so be on the lookout ;P
    MSVC++~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM