2D array passed as a pointer problem

So I am creating a grid in which I want to be able to paste on smaller
grids using a function.

It uses a 2D array for both grids but I am having a problem when it comes
to calling the funciton.

I looked up different ways and passing the array as a pointer seems the best
option but I am still stuck.

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
int gridLayout[20][30] =
{
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
int shipLayout[6][15] =
{
	{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0}, // a grid of the ship i want to place
	{0,1,1,1,1,0,1,1,1,1,0,0,0,0,0},
	{1,1,1,0,1,1,1,1,1,1,1,1,1,1,1},
	{1,1,1,0,1,1,1,1,1,1,1,1,1,1,1},
	{0,1,1,1,1,0,1,1,1,1,0,0,0,0,0},
	{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0}
};
void placeOnGrid(int x, int y,  int** whatToPlace) 
{
	for(int j = 0; j < sizeof(whatToPlace); j++)
	{
		for(int i = 0; i <sizeof(whatToPlace[0]); i++)
		{
			gridLayout[y+i][x+j] = whatToPlace[j][i];
		}
	}
	
	
}


and here is how i am calling the function

 
placeOnGrid(4,4, shipLayout);


i get a compiler error saying "cannot convert int*[15] to int **"

Any help would be much appreciated.

Thanks,
Zecbmo
Last edited on
I managed to find a solution for anyone having a similar problem by using a 2D vector for the shipLayout.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  std::vector< std::vector< int > > shipLayout = {
    { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, // a grid of the ship i want to place
    { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 }
};   

void placeOnGrid(const int x, const int y, const std::vector< std::vector < int > > whatToPlace)
{
    for (int j = 0; j < whatToPlace.size(); j++)
    {
        for (int i = 0; i < whatToPlace[0].size(); i++)
        {
            gridLayout[y + j][x + i] = whatToPlace[j][i];
        }
    }
}



Thanks for posting the answer, I was looking forward to know how to do something a bit similar, this sure will help.
Topic archived. No new replies allowed.