# Thread: Initializing an Array to Zero

1. ## Initializing an Array to Zero

I am trying to initialize a two-dimensional array to 0. Here is my code for the function.

I keep getting an error telling me I am re-initializing the array. I need to set the array magic[25][25] = 0.
Here is my code. The help would be greatly appreciated.

void magicSquare( int size, int magic[25][25])
{
/*local definitions*/

int row = 0;
int col = size / 2;
int c;
int magic[25][25] = {0};

magic[row][col] = 1;

for(c=2; c <= size * size; c++)
{
row --;
col++;

if(aboveTop(row,col,size))
row = size - 1;
if(outRight(row,col,size))
col = 0;
if(filledSquare(row, col, size))
col = size - 1;
}magic[row][col] = c;
return;
}

2. Take a look at the parameters you've passed and the parameters which you declare within the function. Especially "magic".

3. ## help maybe

So this doesn't look like your code too much but I think it might work just as well.

int magic[25][25];
memset( magic, 0, sizeof(magic));

or

int i = 0;
int magic[25][25];
for(i=0;i<sizeof(magic);i++){
(magic + i) = 0;
}

I dunno, but they might work for what you're doing...

good luck

~Gm

4. Question:

Is the function to generate the magic square of odd sizes, if so I have some code to help.

For this
void magicSquare( int size, int magic[25][25])

I would use

void magicSquare (int size, int magic[][25])

Also, a size 25 magic square may not fit on the screen completely. I think I can get up to a size 15.

jc

5. Code:
```void magicSquare( int size, int magic[25][25])
{
/* local definitions*/

int row = 0;
int col = size / 2;
int c;
int magic[25][25] = {0};```
I'm surprised your code compiles. You can't have a local variable
who's name is the same as a parameter's name.

Quzah.

as you are using new style for function declaration you cant again redeclare the same array name again,it will give comile time error.

7. If all you want is to initialize the array to zero, why not try this:

Code:
```...
for(i=0;i<25;i++)
for(j=0;j<25;j++)
magic[i][j] = 0;
...```
seems relatively straightforward. There are other faster, more advanced techniques, but this will do what you want.