Archived

This topic is now archived and is closed to further replies.

TobiasS

Problem with pointer to multidimensional array on heap.

Recommended Posts

TobiasS    122
Hello, I''m pretty new to programming c++ and could use some help with a problem I''ve run into. I''m trying to do a tic-tac-toe (I think that''s what it''s called in English) DOS game and what to use a pointer to an array of an enum that represent the board. If have defined it as following: COLOR (*Board); And then I try to initialize it: Board = new COLOR[*iRows][*iCols]; for(int i = 0; i < *iRows; i++) for(int j = 0; j < *iCols; j++) *Board[j] = new COLOR(EMPTY); When I compile it I get these errors 1) ''='' : cannot convert from ''enum COLOR (*)[3]'' to ''enum COLOR *'' 2) binary ''['' : ''enum COLOR'' does not define this operator or a conversion to a type acceptable to the predefined ope rator What is it I have done wrong? Any help appreciated, thx.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Try something like this:

COLOR **array;
array = new COLOR*[iRow];
for (int i = 0; i < iRow; i++)
array = new COLOR [iCol];


my int''s are int*''s. You''ll have to change that for yours.

Share this post


Link to post
Share on other sites
TobiasS    122
Is there no way to create a pointer to a multidimensional array on the free store rather than creating a pointer to a single dimensional array of pointers that are each pointers to single dimensional arrays on the free store. Not that it doesn''t work. I just think it seems a bit far-fetched.

Share this post


Link to post
Share on other sites
CaptainJester    523
quote:
Original post by TobiasS
Is there no way to create a pointer to a multidimensional array on the free store rather than creating a pointer to a single dimensional array of pointers that are each pointers to single dimensional arrays on the free store. Not that it doesn''t work. I just think it seems a bit far-fetched.


No there isn''t. That''s how you create a 2 dimensional array dynamically. Once it is created though, you can access it just like a normal 2 dim array.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Yes there is, you can allocate a single buffer that is large enough to hold the pointer-map and the actual data - then cast everything into place. RAM is RAM.

Creating an array of an array of pointers is straight forward, but inefficent.

If the size doesn''t need to change at run-time, you can use a template.

Share this post


Link to post
Share on other sites
TobiasS    122
Heh, never thought of it even though it's a pretty obvious solution. I'll probably be using that instead. Thanks Magmai Kai Holmlor for that valuable piece of info!

Anyway, I've run into another problem with the early solution. Here's the code I use:
  
CBOARD::CBOARD(int rows = 3, int cols = 3)
{
iRows = new int(rows);
iCols = new int(cols);
Board = new COLOR*[*iCols]; //declare and initialize tic-tac-toe board

for(int i = 0; i < *iCols; i++)
{
Board[i] = new COLOR[*iRows];
for(int j = 0; j < *iRows; j++)
Board[i][j] = EMPTY;
}
}

At line 8 I'm giving Board[ i ][j] the value EMPTY. Originally I wrote *Board[ i ][j] but got a compiler error basically telling me to remove the asterisk. Since you're not supposed to give the address the value the only conclusion I can come up with is that the second dimension is not really a pointer but rather just a normal variable of type enum on the stack. Please explain what I'm missing here?

[edit: source tags, and [ i ] (without the spaces) means italics]


Edited by - Magmai Kai Holmlor on February 1, 2002 7:29:21 PM

Share this post


Link to post
Share on other sites
Shannon Barber    1681
quote:

iRows = new int(rows);


You should ~*NEVER*~ need to do that in C/C++.
Just set iRows = rows; and make iRows an int, not a pointer to an int.

If it's a Tic-Tac-Toe board, just hard set it to [3][3]

Edited by - Magmai Kai Holmlor on February 1, 2002 7:33:13 PM

Share this post


Link to post
Share on other sites
TobiasS    122
Yeah, I know I shouldn''t use int*, only did so for the extra practice.
The problem with a 3x3 tic-tac-toe board is that the player who start always wins.

Thanks for helping me with the tags.

Share this post


Link to post
Share on other sites
Peter19852001    126
I don''t understand why you would want to create
a dynamic array to do tic-tac-toe.

I ''ve written it before and I use

BYTE board[3][3];

or

BYTE board[9];

and they work fine for me.

Q:"Why it doesn''''t work?"
A:"There is a mistake."

Q:"Who made that silly mistake?"
A:"The one who write it."

Share this post


Link to post
Share on other sites