problem with global variable, help pls.

i have an assignment, which is required to do a maze game.
but i have a problem with global variable.
the question in brief is that i can print out the char 2d-array if i declare it as a global variable but it is not good to use global variable.

i am new to c++, hope someone can help me, thanks in advance :)

how should i code it?
i am really stuck at this
Last edited on
char maze[][1] should be char maze[][7] and you don't pass maze to moveMaze at all. Also, maze[][] is not the right way to pass an array. Plain maze is what you need.
You don't suck. :/

Do you see how you declared your printMaze() function? That number in the square brackets needs to be 7 (the length of the second dimension of your array), not 1.

Also, although some compilers support void main(), your main function should be int main().

EDIT: Ninja'd.

-Albatross
Last edited on
the 1 in char maze[][] is a mistake, i put in the wrong number.

now i know the problem, it is how i pass in the array

thanks anyway, at least u are nice enough to answer my question
Hamsterman already answered your question (I think), but I'll rehash it.

The [][] behind the name of an array are not necessary when you pass it, in fact I think (I've never tried) I think this will result in a syntax error. Remove them and it should work just fine.

-Albatross
yes, it is working now :)
sorry i have another question
for this code i am using a 7*7 arrays,
so when i pass the array i would write char maze[][7] (in other function's parameter),
but what if i used 10*10 array, for example, how should code it?

this is the working code
Last edited on
Just change all the [7]s to [10]s, and make sure that everywhere you call printMaze that the second two arguments are now 10.

-Albatross
no, that is not what i meant.
u declare an array of any size, then i do not have to change any code in other functions.. something like this
(because later i will have to do a header file and each function as a cpp file)
If you want to be able to pass any array to the function, you'll have to stick to 1d ones. Notice that arr2d[x][y] is the same as arr1d[x+y*width]. Don't forget to pass width and height with the array.
do u mean that i use x and y to represent the number?
like
int x = 7;
int y = 7;
char array[x][y];

then in other function's parameter,

void function (array[][y], int row, int col)

i tried this, but i get errors,
one of them is y is undeclared identifier
1
2
3
4
5
6
7
8
9
10
11
12
char array[7*7] = {...};
print ( array, 7, 7 );

...

void print( char array[], int width, int height ){
   for (int x = 0; x < width; x++){
        for (int y = 0; y < height; y++)
            cout << array[x+y*row] << " ";
            cout << "\n";
    }
}
cool, i got what u mean and i think it is working.
thanks for your time, appreciated :D
Topic archived. No new replies allowed.