0

So I already saw this C++ Passing multi-dimensional arrays into functions but it says to put the value of the Column in for the parameter.

For a 3x3 array does that mean saying

void printGrid (char ticTacToeBoard[][2])

or

void printGrid (char ticTacToeBoard[][3])

When trying to pass it through?

Community
  • 1
  • 1
ldehart
  • 5
  • 3
  • possible duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function) – Ken Boreham Oct 09 '14 at 00:23

3 Answers3

0
void printGrid (char ticTacToeBoard[][3])
Ken Boreham
  • 849
  • 6
  • 16
0

With

char board[3][3];

the prototype would be

void printGrid(const char (&ticTacToeBoard)[3][3]);

and call it that way:

printGrid(board);
Jarod42
  • 173,454
  • 13
  • 146
  • 250
  • Can you explain exactly why you would use your prototype as opposed to Ken's? Not to say anything is wrong with his, but that is the way all the stuff I was reading seemed to point. My knowledge on when you use the & is pretty poor and all, so just curious why you'd use the const and the &ticTacToeBoard. – ldehart Oct 08 '14 at 23:57
  • @Idehart: I set the 2 dimensions of the array whereas `char ticTacToeBoard[][3]` (which is equivalent to `char (*ticTacToeBoard)[3]`) only set one dimension (and so is less typed). And I add `const` as I expected that `printGrid` doesn't change board content. – Jarod42 Oct 09 '14 at 00:03
  • Thanks for the fast response, that makes a lot of sense! What's the purpose of using &ticTacToeBoard as opposed to just ticTacToeBoard? – ldehart Oct 09 '14 at 00:09
  • @Idehart: The syntax to pass array is not intuitive, if you just do `void printGrid(char board[3][3]);`, it will be in fact `void printGrid(char board[][3]);` (and so `void printGrid(char (*board)[3]);`). Using `std::vector` or `std::array` is really more intuitive. – Jarod42 Oct 09 '14 at 00:15
0

void printGrid (char** ticTacToeBoard)