i'm doing sokoban with c++, with level select, i found out that i only can declare map at global, am i able to create function with function in it?
exp
void map1()
{int map[11][11]=// map structure
void drawmap() //draw map
void playgame() // move up and down
To pass that to a function, then you prototype the function like so. void function(int map[11][11]);
Though you can also omit the left-most dimension. void function(int map[][11]);
You can even turn it into pointer notation, so long as you make sure the () are in the right place. void function(int (*map)[11]);
To call the function, you just supply the variable name of your Nx11 array. function(map);
In the function implementation, you use the same notation as if the array was in scope.
1 2 3 4 5 6 7
void function(int map[11][11]) {
for ( int r = 0 ; r < 11 ; r++ ) {
for ( c = 0 ; c < 11 ; c++ ) {
map[r][c] = 0;
}
}
}
Again, it doesn't matter if you used the [][11] or (*)[11] variants above.
This should give you an idea of how not to declare "map" as a global variable and is a different way of saying what salem c has said.
Be careful when naming variables. In the standard namespace there is "std::map", but uou need the header file "<map>" to use it. Otherwise it is confusing at first look at your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include // the include files you need.
constexpr size_t MAPSIZE{ 9 };
void map2(int board[MAPSIZE][MAPSIZE]) // <--- Array received by function.
{
// <--- Your code here.
}
int main()
{
int board[MAPSIZE][MAPSIZE]{}; // <--- Define array here.
map2(board); // <--- Pass array to function.
return 0;
}
Should your rows and columns be different you could do something like: