how to pass a matrix to a function?

I'm working on a 6x6 checkers program for AI class,
I'm trying to pass the states as matrices,
for example, starting state is the matrices

1
2
3
4
5
6
7
8
9
10
int matrix[6][6]=
    {
        {0,2,0,2,0,2},
        {2,0,2,0,2,0},
        {0,0,0,0,0,0},
        {0,0,0,0,0,0},
        {0,1,0,1,0,1},
        {1,0,1,0,1,0}
    };


and I'm trying to pass it to the function

1
2
3
4
5
6
7
8
9
void printBoard(int** matrix)
{
    cout<<"|"<<matrix[0][0]<<"|"<< matrix[0][1]<<"|"<<matrix[0][2]<<"|"<<matrix[0][3]<<"|"<<matrix[0][4]<<"|"<<matrix[0][5]<<"|"<<endl;
    cout<<"|"<<matrix[1][0]<<"|"<< matrix[1][1]<<"|"<<matrix[1][2]<<"|"<<matrix[1][3]<<"|"<<matrix[1][4]<<"|"<<matrix[1][5]<<"|"<<endl;
    cout<<"|"<<matrix[2][0]<<"|"<< matrix[2][1]<<"|"<<matrix[2][2]<<"|"<<matrix[2][3]<<"|"<<matrix[2][4]<<"|"<<matrix[2][5]<<"|"<<endl;
    cout<<"|"<<matrix[3][0]<<"|"<< matrix[3][1]<<"|"<<matrix[3][2]<<"|"<<matrix[3][3]<<"|"<<matrix[3][4]<<"|"<<matrix[3][5]<<"|"<<endl;
    cout<<"|"<<matrix[4][0]<<"|"<< matrix[4][1]<<"|"<<matrix[4][2]<<"|"<<matrix[4][3]<<"|"<<matrix[4][4]<<"|"<<matrix[4][5]<<"|"<<endl;
    cout<<"|"<<matrix[5][0]<<"|"<< matrix[5][1]<<"|"<<matrix[5][2]<<"|"<<matrix[5][3]<<"|"<<matrix[5][4]<<"|"<<matrix[5][5]<<"|"<<endl;
}


and pretty sure i'm not passing the matrix right, help?
Last edited on
The problem is you are trying to accept the matrix as a pointer to a pointer which is not exactly what a multidimensional array is (it's a pointer to an array). There are several conversations about this that you can find on google so I'm not going to repeat everything here.

I would suggest you do this instead
void printBoard(int matrix[][6])
Everything apart from the size of the first subscript is needed.
And it would be easier to use nested for loops to do the printing.

HTH
Topic archived. No new replies allowed.