I'm making a tic-tac-toe game and was wandering if you could pass an array as a parameter? I want to create a header to create a function called drawBoard but need the array to be accessed from the function. Here is what the header looks like right now:
Yep, you can pass it. If you created the array like this:
int GameBoard[3][3];
... then your function will look like this:
1 2 3 4
void drawBoard(constint GameBoard[3][3])
{
... function body ...
}
or
1 2 3 4
void drawBoard(constint GameBoard[][3])
{
... function body ...
}
... since the compiler would already know the first dimension anyway. But there's nothing wrong with reminding yourself and preventing errors. And then you'd call:
drawBoard(GameBoard);
... wherever you display the board in your program.
You'd take away the 'const' if you wish to be able to change the elements from inside the function. There are other ways to pass arrays, depending on their dimension. Instead of explaining it here, I'd recommend you simply take a look at the existing tutorial section: