• ### Announcements

#### Archived

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

# dynamic 2d array

## Recommended Posts

I am trying to figure out how to declare a dynamic 2d array and use it. My book says to use: note: in my class MagicSquare, I have double **pMagicSquare as the declaration


void MagicSquare::Generate(void)
{
int Row_Index = 0, Col_Index = Col_Size / 2, current_count = 1;

pMagicSquare = new double* [Col_Size];
for(int i = 0; i < Col_Size; i++)
pMagicSquare[i] = new double[Row_Size];

memset(pMagicSquare, 0, sizeof(pMagicSquare));

pMagicSquare[Row_Index][Col_Index] = current_count++;

I get an error saying I can't access pMagicSquare[0][1] when I make it a 3x3 2d array. I don't understand what I've done wrong here, and have been messing with it for three days now. [edited by - xg0blin on September 1, 2002 2:51:22 PM]

##### Share on other sites
First of all I''d like to ask you to specify the compiler error you get, it will be easier to debug.

Second of all, the only lead I have by glancing at it quickly is the
Col_Index = Col_Size / 2
and
pMagicSquare[Row_Index][Col_Index] = current_count++;

Now, I have no idea how you built your program, but Col_Index could generate unsuspected compiler errors if its value turns out to be a floating-type.
I mean, this doesn''t work: array[1][2.35] = x;

I don''t know... really I don''t. I''m just too tired, heh.

• ### Forum Statistics

• Total Topics
627735
• Total Posts
2978854

• 10
• 10
• 21
• 14
• 12