In our c course, the teacher has given us a mini project to build a "Reversi" game. I'm having trouble building the board.
#define Size 8
int main()
{
char Board[Size][Size] = { {" "} };
resetBoard(Board);
printBoard(Board);
printf("\n");
getch();
}
void resetBoard(int** board)
{
for (size_t i = 0; i < Size; i++)
{
for (size_t j = 0; j < Size; j++)
{
*(board + (i * Size + j)) = 'x';
}
}
}
void printBoard(int board[Size][Size])
{
for (size_t i = 0; i < Size; i++)
{
for (size_t j = 0; j < Size; j++)
{
printf("%c", board[i][j]);
printf(" ");
}
printf("\n");
}
}
I've checked the program and the program gets:
Run-time check failure #2 - Stack around the variable 'Board' was corrupted
when it changes the first X on the third row. For example, if I run the program till the end of the 2d row (16), I wont get this error.
I think there may be problem with initialization of your board as type of char and working with pointers and array of type int in your functions. Char has size of 1 byte and int has bigger size depending on platform (4 bytes most of the times). This will cause memory problems with manipulation and looping over your array.
In your case, it looks like you have looped over whole allocated memory after 2 rows because you used pointers of type int. Int is in your case probably 4 times bigger than char resulting in looping over whole of your data structure of type char 4 times quicker as you expected.