retreiving elements from a pointer to 2darray (blah[][])

Hi I'm intending to create and store a pointer (to store in GameBoard::m_panGameBoardArray) pointing to a 2d array (anBoard[][]). Send that pointer as an argument to function (in GameBoardRenderer) and print out (cout) the contents of the 2d array.

Stripped down code for ease of read

GameBoardController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void GameBoardController::Play(GameBoard * pGameBoard){

	bool bIsGameOver=false;

	GameBoardRenderer * pGameBoardRenderer = new GameBoardRenderer();

	while(bIsGameOver==false)
	{		
		pGameBoardRenderer->PrintBoard(pGameBoard);
		system("PAUSE");
	}


}


Gameboard.h
1
2
3
4
5
6
7
8
9
10
11
12

class GameBoard
{
private:
	int * m_panGameBoardArray;

public:
	void ** GetGameBoardArrayPointer();

	void SetGameBoardArrayPointer(void * panGameBoardArray);

};


GameBoard.cpp
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

GameBoard::GameBoard(int*panSettings)
{
	const int height=8,width=8;

	int anBoard[height][width];

	SetGameBoardArrayPointer(anBoard);

	//Fill with 0
	for(int i=0;i<width;i++)
	{
		for(int j=0;j<height;j++)
		{
			anBoard[i][j]=0;
		}
	}
}

void ** GameBoard::GetGameBoardArrayPointer()
{
	return (void**)m_panGameBoardArray;
}

void GameBoard::SetGameBoardArrayPointer(void *panGameBoardArray)
{
	if (panGameBoardArray)
	{
		m_panGameBoardArray=(int*)panGameBoardArray;
	}
	else
	{
		cout << "CANNOT SET GAME BOARD ARRAY POINTER 0";
	}
}


GameBoardRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void GameBoardRenderer::PrintBoard(GameBoard * pGameBoard)
{
	
	int ** m_pGameBoard= (int**)pGameBoard->GetGameBoardArrayPointer();
	
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
                	cout << m_pGameBoard[i][j];
		}
		cout << endl;
	}

}


Help appreciated :)
Sorry, Your method about passing 2D array can't work.
In your method, you pass a 2D array to a function with "void*" parameter. In this function you cast "void*" to "int*". in another function, you cast "int*" to "int**", then print this 2D array. I write some code to imitate your method, it is easy to see what's wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Fun(void* array)
{
    int* array1=(int*)array;
    int** array2=(int**)array1;

    cout<<&array2[0][0]<<endl; //became 0x1, it's not right.

    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            cout<<array2[i][j]<<' ';
        cout<<endl;
    }
}


1
2
3
4
5
6
7
int main()
{
    int array2D[3][3]={1,2,3,4,5,6,7,8,9};
    
    Fun(array2D);
    return 0;
}


So I prefer processing a 2D array like 1D array. just like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Fun2(int* array,int w, int h)
{
    int* pa=array;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cout<<*pa<<' ';
            pa++;
        }
        cout<<endl;
    }
}


1) You can't mix straight 2D arrays (int foo[x][y]) with dynamically allocated 2D arrays (int** bar), as the two are entirely different under the hood and cannot be interchanged.

2) Do not use void pointers as they mask illegal casts and let code compile without error, even though that code will explode when you try to run it.

3) You're having this trouble because multidimensional arrays are evil. See this thread:

http://cplusplus.com/forum/articles/17108/

In addition to explaining why they're evil, it also gives examples of proper syntax with using them. It also has alternative suggestions that are more sane.
Thanks. This advice was quite useful.

I am however going dump arrays for this to use a vector of vectors and hide the data structure in it's own class.
Topic archived. No new replies allowed.