Error in my function?

Hey guys I'm trying to write a code for a connect4 program, and it seems that one of my functions don't work when I debug it.

bool DisplayBoard(int** connectNBoard, int numRowsInBoard)
{
int row;
int col;

//outputting the board array
for(row=0; row<=numRowsInBoard; row++)
{
for(col=0; col<=numRowsInBoard; col++)
{
cout << setw(3) << connectNBoard[row][col];
}
cout << endl;
}

return 0;
}

On the cout << setw(3) << connectNBoard[row][col]; line, the error says "Unhandled exception at 0x01391864 in Connect.exe 0xC0000005: Access violation reading location 0xcccccccc.
What might the problem be?
You're stepping out of bounds of your array.

Either you're passing the array incorrectly, or you're going too far, or both.

I would bet that those <= are supposed to be <
changing the <= to < didn't work..
then you're probably passing the array incorrectly. How are you calling this function?
myConnectNBoard = new int [numRows];

DisplayBoard(&myConnectNBoard, numRows);
yep, that's wrong.

myConnectNBoard = new int [numRows];

This gives you a 1D array. Basically you have a 1 x R array of ints (where 'R' = numRows)

The function you're passing it to then treats it as if it were a C x R array. Of course this second dimension doesn't exist so it's making your program explode.



Multidimentional arrays are actually quite confusing. See this link for how to use them and for the many reasons why you shouldn't use them:

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

Topic archived. No new replies allowed.