views:

42

answers:

2

I am currently developing a game for the iPhone/iPod/iPad. To store the board data, which is 12 columns by 8 rows, I have an array that stores pointers to one of the game items, a block. It is declared as follows:

BlockData* mBoard[kNumberOfColumns][kNumberOfRows];

I also have another array declared like this:

BlockData* mCenterSquare[16];

This I want to store pointers to specific locations on the board, for example,

mCenterSquare[0] = mBoard[4][5];

Is this the right way of doing it? What I want to accomplish using the above line of code is for mCenterSquare[0] to store a pointer to a position on the board - 4th column and 5th row. How would I go about doing this?

Thanks in advance.

A: 

I can't help but think this is a trick question. Yes, that is the way you would do it. :)

Brent Arias
It doesn't compile though . . .
macdude93
@macdude93: It is legal syntax (and it compiles for me). Is BlockData a typedef? What is the compilation error you are getting?
Brent Arias
My mistake - I feel stupid. There wasn't a ; after the last statement. Ridiculous. I didn't even look at the error before posting this. Had to have been tired.
macdude93
+1  A: 

Why not only use the mCenterSquare array to store data:

function BlockData* getBlockData(int row, int col) {
     return mCenterSquare[row * kNumberOfCols + col]
}
Avall
Whate does row * kNumberOfCols + col supposed to do?
macdude93
Find a unique index for every {row, col} pair.
Avall