How do you pass a two-dimension INT array between functions?

I have forgotten the simple C method for passing an array of INTs between functions - and I'm not near my library where I could look it up without shame.  So here we go:

I have a two-dimension array, playerBoard, that describes a 20x20 playing board:

      int playerBoard[20][20];

...and I need to send it to a function (actually, quite a few functions) to initialize it to -1's, and later to place playing pieces, display the results to an ASCII terminal, that sort of thing.  At present, the preferred method is to make the int array global, and let everybody/everything have a crack at it.  I'm looking for a more secure (data-wise) method to pass the information back and forth.  (Dopey me, *I* know that the data stays in the same place, that a pointer to the location is all that gets passed - but it's all lost in the antiquity -and chaos - of age...)

Does anybody have any suggestions?  (Just thought I'd ask...)
LVL 1
LongFistAsked:
Who is Participating?
 
IndrawatiCommented:
If you really want to make sure that board is declared as

int board[20][20];

you can do:

void foo(int (&param)[20][20])
{
}

Now if you declare board as anything other than int[20][20], compiler will complain.
0
 
IndrawatiCommented:
Here's an example:

void foo(int param[20][])
{
}

int main()
{
      int board[20][20];
      foo(board);
}
0
 
IndrawatiCommented:
Sorry, foo should be:

void foo(int param[][20])
{
}
0
 
LongFistAuthor Commented:
So, if I read you right, the previous declaration:

    int foo (int param[][20]);

   ...would allow me to pass a two-dimensional int array of [n] first dimensions, as long as the second dimension was 20, right?  And I would still simply call it like:

   foo(board);

   ...to make it "do it's thing".  Right?  Or am I still confused?

0
 
LongFistAuthor Commented:
This really rocks!  I took the definition a little farther by setting the board dimensions via [coinst int]s - that way, they always match, no matter how large (or small) the board needs to be!

Thanks again!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.