funcion help

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

If you have an array, say
int map[11][11];

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.
thanks salem c bro for reply,
now my problem is i dont want declare int map at global,
my idea 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
25
26
27
28
29
30
31
32
void map1()
{
int map[9][9]={//structure}
void map2()
{
int map[9][9]={//structure}
}
void map3()
{
int map[9][9]={//structure}
}
.
.
.
.
.
void drawmap()
{//draw map}
void playgame()
{
// player control character up,down,left,right
}
void level1()
{
map1();
drawmap();
playgame();
}

int main()
{
level1();


am i able to do that? i tried squeeze them into one function but wont work, thanks in advance
Hello blacksjacks,

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:
1
2
constexpr size_t MAPROW{ 9 };
constexpr size_t MAPCOL{ 11 };


Hope that helps,

Andy
thanks Handy Andy and salem c, i had made an progression on my programs~ thanks
Topic archived. No new replies allowed.