I'm Horrible at Passing Arguments ):

Recurring problem, sigh.

This time I want to pass char Map[Y][X] into a function. I can get reasonably close kinda-sorta, but alas. This was my guess:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    void ShowMap(*MrMap)
    {
        //...
    }
    
    int main()
    {
        char Map01[40][81] = {/*...*/};
        
        ShowMap(Map01);
    }

/*
    Also tried:
    void ShowMap(MrMap)
    void ShowMap(&MrMap)
*/

Plus I get

G:\Maps\main.cpp|162|error: invalid types 'char[int]' for array subscript|

.. when I try to use switch(MrMap[Y][X]) within the function.

Note: It has to be type char because the entire program I've made relies on manipulation of individual characters, so string is of no use here.

I'm plodding along.. thanks for the help.
Try :
1
2
3
4
5
6
7
8
9
10
11
 void ShowMap(*MrMap)
    {
        //...
    }
    
int main()
    {
        char Map01[40][81] = {/*...*/};
        
        ShowMap(*Map01);
    }
Last edited on
No dice.

G:\Maps\main.cpp|97|error: invalid conversion from 'char*' to 'char'|

That, or some variation thereof - depending on what I try.

EDIT: Oh hey it works if I send in *Map01 and take *Map01, but I'm still having the same problem inside of the function as earlier:

G:\Maps\main.cpp|162|error: invalid types 'char[int]' for array subscript|


Here's what I'm trying to do in there:

1
2
3
4
5
6
7
8
9
10
11
12
13
    void ShowMap(char *Map, const int Y, const int X, int Terrain)
    {
            for(int j = 0; j < Y; j++)
            {
                for(int i = 0; i < X; i++)
                {
                    switch(Terrain)
                    {
                        case FOREST:
                        switch(Map[j][i])
                        {
                            case '#': //...
  // etc. 
Last edited on

1
2
void ShowMap(*MrMap)  // <- this is nonsense, you have to specify the type of the parameter
//  is it an int?  an int array?  what? 


what you might want is this:

1
2
3
4
void ShowMap(char MrMap[][81])
{
    //...
}


and then you'd call it how you'd expect:

 
ShowMap( Map01 );  // <- note, no * symbol 
Last edited on
Oh my bad, I already had void ShowMap(char MrMap).

Your method didn't work for me:

G:\Maps\main.cpp|97|error: cannot convert 'char (*)[81]' to 'char*' for argument '1' to 'void ShowMap(char*, int, int, int)'|

.. but the pointer way did.

1
2
3
4
5
6
7
8
9
10
11
    void ShowMap(char *Map)
    {
        //... 
    }
    
    int main()
    {
        char Map01[40][81] = { /*...*/ };
        
        ShowMap(*Map01);
    }

I'm still trying to get the thing to work inside of the function.
Last edited on
Disch is right,
void ShowMap(char MrMap[40][81])
{
//...
}

int main()
{
char Map01[40][81] = {/*...*/};

ShowMap(Map01);
}

should work.. supposing you already know the sizes of the array
You got it backwards timmy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ShowMap(char MrMap)
{
    // This "works", but outputs a single, random char.
    cout << Map << "\n\n";
    
    // I need this, it doesn't work.
    cout << Map[12][58] << "\n\n";
}

int main()
{
    char Map01[40][81] = { /*...*/ };
    
    ShowMap(Map01[40][81]);
}


I'm still getting this error, gah.

G:\Maps\main.cpp|160|error: invalid types 'char[int]' for array subscript|

It's quickly becoming the bane of my life.
Disch has already shown you how to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
void ShowMap(char MrMap[][81])
{
   cout << MrMap[12][58] << "\n\n";
}

int main()
{
    char Map01[40][81] = { /*...*/ };
    
    ShowMap(Map01);

   return 0;
}
Oh man, I got confused because it didn't work using MrMap[Y][X] like timmy said. Thanks a bunch, it works perfect.
Topic archived. No new replies allowed.