Jump to content
  • Advertisement
Sign in to follow this  
Mas0n

Passing 2d Arrays through Functions

This topic is 4773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm having trouble figuring out how to pass a 2d array through a function. I'm using this function to decide either to print an x or an o depending on the player. rIndex is the row and cIndex is the column in my array and boards my 2d array that I would Like to pass through. This block of code is used in my program when i start subscripting through the array to refresh what the board looks like. My failed attempt is... // Check the Board void checkBoard(int rIndex, int cIndex, int board[][]) { if (board[rIndex][cIndex] == 1) // Prints X { cout << "X"; } if (board[rIndex][cIndex] == 0) // Prints O { cout << "O"; } } When I run this, I get a compile error saying "error: Declaration of 'board' as multidimensional array must have bounds for all dimensions except the first. Can anyone help me please?

Share this post


Link to post
Share on other sites
Advertisement

void checkBoard(int rIndex, int cIndex, int board[][])
.
.
"error: Declaration of 'board' as multidimensional array must have bounds for all dimensions except the first.


Its exactly what it sounds like- when you declare your array, you must tell the compiler how many variables big (i dont know the technical term for it) it is.

This would be legal:

int board[][5];
or
int board[8][1231];

How much do you know about pointers? If you are comfortable with them, you should just use one, instread of passing the array:


void checkBoard(int rIndex, int cIndex, int *board)
.
. // later
.

*board[ row * NUMBER_OF_ELEMENTS_IN_A_ROW_WHICH_GETS_THE_"Y"_OFFSET + column] = whatever you want.




If you are not comfortable with this, just make your board array global and forget about it.

Share this post


Link to post
Share on other sites
Basically the compiler is saying that it doesn't know what size the array is. If the array is a fixed size, you can just provide the size in the function declaration. Otherwise you'll have to pass a pointer to the array instead.

Share this post


Link to post
Share on other sites
Thanks guys, I got that part working now :). I didn't use the pointers yet though. I only know how to use them in subscripting and a few other things. But now I have one more problem. I'm Trying to call the function while I subscript the array. Heres what I tryed...

// Write New board to screen
for (size_t cIndex = 0; cIndex != col; ++ cIndex)
{
checkBoard(1, cIndex, board[][3]);
}

When I ran this code, I got an error saying "error: expected primary expression before ']' token" I think it has something to do with the way I'm trying to pass board through the function, but I'm not sure. I toyed around with it for about half-hour but can't seem to see the problem.

Btw, when you guys help me, am I supposed to add to your rating? I'm new to these forums and I've never seen that before. Thanks for all the quick help so far!

Share this post


Link to post
Share on other sites
What your code is trying to do here is pass an element of the board into the function instead of the board itself. You should just pass board without the subscripts since board is a int [3][3] and the function expects a int[][3]. With the subscripts you're trying to send a just one int (though its not working since tthe first subscript is undefined).

To summarize:

checkBoard(1, cIndex, board[][3]); should be
checkBoard(1, cIndex, board);

You should look into using pointers for passing array data or, better yet, make Board a class with the array inside and provide functions for accessing the data.

About the ratings, you could rate someone up if you find them helpful, but it's not required and noboddy will care if you don't.

EDIT: Ooops, i thought you were using chars instead of ints to hold your data. Fixed now.

Share this post


Link to post
Share on other sites
Sweet thats all I needed :). Thanks for all your help guys. Btw, I tryed to give a good rate, but it only affected one person in this thread seeing how he did have 996. But really thanks for all the help!

Thanks,
Mason

Edit:
Just got done with tic-tac-toe and it works good :). Im sure I used way too many lines of code seeing as there is faster ways to do it, but it does its job. Didn't really bother with graphics seeing is how im in a console window lol. Thanks again

[Edited by - Mas0n on July 27, 2005 2:34:48 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!