how can i call a multidimensional array reference in a function...i have this code and i get the error...

cannot convert char* to char (*)[20] for argument 1 'void proximity (char (*) [20])'

i get the jist but somethings wrong heres my code with bits cut ouut.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
oid proximity (char brdray [20][20]); // the prototype

int main(){
brdray [10] [10] = b;
brdray [5] [5] = u;

    while (1)
{
 system ("cls");
getboard();
move();
proximity(&brdray[20][20]); //this is the function being called
system ("pause");
}



}

void getboard ()
{
    for (int a=0;a<20;a++)
    {
        for (int b=0;b<20;b++)

            cout<<brdray [a][b];
            cout<<endl;

    }
}



void proximity (char brdray[20] [20]) // the function


///here i would have some for loops that would cycle through the char to find if
///specific chars or char names were in there




L12: You're passing the address of the array.
L1,34: proximity is declared to receive the array by value.
This discrepancy is causing your error.

You want to pass the array by reference:
L12:
 
proximity(brdray[20][20]);


L1,34:
 
void proximity (char &brdray[20] [20]) 

Remove the & on line 12.
well now its complaining about being an array of references, i actual tries not to pass by reference and proximity had just (array) ass its argument but...

maybe my function is innefective, i dont see how though here it is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void proximity (char brdray[20] [20])
{
    int n,m,k,l;
    int j =0;
    int s =0;
    for (s;s<20;s++)
    {
        for (j;j<20;j++)
        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
        if ((brdray [s] [j]==b)&&(brdray [m][j]==u))
        {
           cout << "once"<<endl;
           system ("pause");
        }

    }
}


it just couts "once" so i know it works and...it dont
The call should look like proximity(brdray);. I'm not sure how I missed that earlier.
well i guess if that is correct im just sure now that theres a flaw in the function...is sometimes hard to work out whats wrong

its not that a difficult code to follow :) just see if the function checks the board gwaaaaaahnn dare ya


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
int posx=10;
int posy=10;

char z='*';
char x=' ';
char b='^';
char u='!';

char brdray [20] [20] = {  {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z}
                                          };

void move ();
void getboard ();
void proximity (char brdray [20][20]);

int main(){
brdray [10] [10] = b;
brdray [5] [5] = u;

    while (1)
{
 system ("cls");
getboard();
move();
void proximity(char brdray [20][20]);

}



}

void getboard ()
{
    for (int a=0;a<20;a++)
    {
        for (int b=0;b<20;b++)

            cout<<brdray [a][b];
            cout<<endl;

    }
}

void move ()
{

int enter;
int a,e,i,o;


brdray [posx] [posy] = x;

a=posx;
i=posx;
e=posy;
o=posy;
a=a+1;
e=e+1;
i=i-1;
o=o-1;


cout << "enter 1,2,3, and 4 to move" <<endl;
cin >> enter;
switch (enter)
{
case 1:
if(brdray [a][posy]!='*')
posx=posx+1;
break;
case 2:
if (brdray [posx][o]!='*')
posy=posy-1;
break;
case 3:
if (brdray[posx][e]!='*')
posy=posy+1;
break;
case 4:
if (brdray [i][posy]!='*')
posx=posx-1;
break;
}
brdray [posx] [posy] = b;
}

void proximity (char brdray[20] [20])
{
    int n,m,k,l;
    int j =0;
    int s =0;
    for (s;s<20;s++)
    {
        for (j;j<20;j++)
        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
        if ( brdray [s] [j]== '^')
        {
           if ( brdray [m] [j]=='!')
           {
               cout << "once";
               system ("pause");
           }
        }

    }
}
Last edited on
Is your proximity function, trying to check if there is anything in an adjacent cell on the board?

It is hard to tell what you are trying achieve by looking at the code.

1
2
///here i would have some for loops that would cycle through the char to find if
///specific chars or char names were in there 


I find this description a bit vague as well.

If you are searching for a specific char somewhere on the board, then I don't see how your code is going to achieve that.

Now some pedantic style police points :+D

while (1)

Try not to use infinite loops like that - there are situations where infinite loops are OK, but this isn't one of them. Try to think of a valid & logical situation where the loop will end (you don't seem to have any at the moment)- make that the condition in the while loop.

Instead of this:
1
2
3
4
5
int j =0;
    int s =0;
    for (s;s<20;s++)
    {
        for (j;j<20;j++)


I would prefer this:

1
2
3
4
5
6
    int j;
    int s;
    for (s = 0; s<20; s++) 
        for (j= 0 ; j<20; j++) {

        }


If the variables j & s are not going to be used outside the loops, then you can do this:

1
2
3
4
    for (int s = 0; s<20; s++) 
        for (int j= 0 ; j<20; j++) {

        }


I also prefer to use variable names like Row & Col instead of s & j. This might save you from an error one day.

This:
1
2
n=n+1;
m=m+1;


Can be written:

1
2
n++;  //works for integers
m++;


With this:

if ((brdray [s] [j]==b)&&(brdray [m][j]==u))

Variables b and u are not defined - your compiler should have told you that.

In future, please post the compiler output as well as your code.



Edit: I took so long to reply - some of the things I said don't apply anymore.

Edit2: Sorry global variables b & u.
Last edited on
You haven't told us why you think anything is wrong with the code. Do you get an error message, or is it not behaving as expected?

I don't get why you have all those variables in proximity. Most of them are not even used. The only statement that's inside the inner loop is n=s;. You have to use curly brackets to include all the statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void proximity (char brdray[20] [20])
{
	for (int s=0;s<20;s++)
	{
		for (int j=0;j<20;j++)
		{
			if ((brdray [s] [j]==b)&&(brdray [j+1][j]==u))
			{
				cout << "once"<<endl;
				system ("pause");
			}
		}
	}
}

I'm not really sure what it's supposed to do but brdray[j+1][j] is not correct because it is out of bounds when j == 19.

You shouldn't be so afraid of using longer, more descriptive names, especially for global variables. I mean b and u doesn't give a clue what they are for. They should probably be constants by the way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void move ()
{

int enter;
int a,e,i,o;


brdray [posx] [posy] = x;

a=posx;
i=posx;
e=posy;
o=posy;
a=a+1;
e=e+1;
i=i-1;
o=o-1;


With that code - none of variables are defined (you haven't set a value for them.

Edit: Some of them aren't declared either, and the function is void - so what were wanting to achieve with that?

Edit 2: My bad some are global variables - itself not a good idea.
Last edited on
ahh okay thanks guys k...

im trying to check if the two chars ^ and ! are adjacent to each other they are outside scope as b and u...i been trying lots of things(including == std::b and char *bpointer), i dont get any error messages, i just dont get the satisfactory cout (unless i try a strange experiment) when the two chars are adjacent on the board

thanks for the advice on tidy readable code, if i was not getting help and was purley self taught i would never learn the common sense that would help...though this code has temporary bits and pieces in it while i build it, i have not learnt how to plan something yet and just get to it

hence the warts

I made it work!! is the call for the function proximity to check the array brdray an elegant solution or a bodge?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void move ();
void getboard ();
void proximity (char *array ,int s, int j); ///is this approach normal

int main(){

bool quit = false;
brdray [10] [10] = b;
brdray [5] [5] = u;
int loop =0;
    while (quit != true)
{
 system ("cls");
getboard();
move();


char *array = &brdray[0][0];

proximity(array,20,20); ///have i over complicated this
loop++;
if (loop==10)
{
    cout << "do you want to quit? (y/n)";
    char yn;
    cin >> yn;
    if (yn == 'y')
    {
        quit = true;
    }
    else if (yn=='n')
    {
        loop = 0;
    }
}
}



}

void getboard ()
{
    for (int a=0;a<20;a++)
    {
        for (int b=0;b<20;b++)

            cout<<brdray [a][b];
            cout<<endl;

    }
}


void proximity (char *array, int s , int j)
{

    int n,m,k,l;

        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
   for (s=0;s<20;s++)
   {
       for (j=0;j<20;j++)
       {
           if (brdray [s] [j]=='^'&&brdray [s+1][j]=='!')
           {
               cout << s<<j<<endl;          ///my function finally works but is this bad practice
               system ("pause");///<- why is that bad practice?
           }

       }
   }



}



i put some sensible bits in i think it is wise to put the good habits even in practicce code im happy with them too...but tell me is it over complicated is it sensible could you do it better i want to know before i close the thread and feel satisfied...thanks for you help guys am lernin
why is it bad to have global variables in that function ideas man? what would the right thing to do be
OK, so now with your proximity function:

You have arguments s & j, but they are set to zero in the for loops.

Variables n,m,k,l aren't ultimately used for anything - that is they don't affect the result in any way.

why is it bad to have global variables in that function ideas man? what would the right thing to do be


This is just one thing I found via Google:
http://stackoverflow.com/questions/484635/are-global-variables-bad


You could declare the variables in main, then pass them to which ever function needs them. If they will never change, make them const.
great i wondered when i would get to use const...remininds me i have never used new or static either
cheers mr ideas man
Topic archived. No new replies allowed.