• ### Popular Now

• 9
• 9
• 11
• 13
• 9

#### Archived

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

# Simple Array Problem

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

## Recommended Posts

You can only initialise the array with the braces when you declare the array. It can't be done afterwards.

##### Share on other sites
1. If you just want to set the array to one value, such as zero, then memset() is the way to go.
2. If you realize that for the compiler to do the desired initialization it must have the data stored in the executable file somewhere anyway, you quickly see that you would use up lots of memory if you are storing many of these blocks in the code itself, (but this is of course unavoidable if the blocks are not able to be generated via some mathmatical function).
3. Once you realize #2, then you see that if this behavior is what you need, just declare another local variable where you want to set the array (using the initialization structure you desire), then copy it into the target via memcpy().
4. If these blocks are many, and may be used in multiple places, you can simply define them in some global area, as you would any other global array, then just memcpy() wherever they are needed. Treat these globals not as variables but instead as constants.
-K.McAfee

##### Share on other sites
I see people make this mistake hundreds of times. And people
still don't learn the basic rules.

Rules for C/C++ declaration are special cases! Namely,
structures, strings, and arrays all have special declaration
cases! They can't be used anywhere else!

However, you may actually be able to do this in syntax, but you
have to create a special C++ overloaded operator to handle the case.
Just look at examples how people do string concantenation with

Just another thought.

##### Share on other sites
c/c++ doesn't allow that...but you can do this-
code:
for(int y=0; y<5; y++){    for(int x=0; x<5; x++)        grid[x][y] = 0;}

[This message has been edited by lshadow (edited October 19, 1999).]

##### Share on other sites
Ok this is a really basic C question. How do I assign values to an array that has already been initialized?

I want to be able to do this:

int grid[][5] = {(0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}};

by doing something like this:

**********************

int grid[5][5];

//blah blah lots of game code here

grid[][5] = {(0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}};

*****************
I get all kinds of syntax errors and such when I do this. Does anybody know how I do this?

I know I can just assign all 25 values individually, but I want to know if there is a faster/better way to do it.

##### Share on other sites

You cannot initialize an array in C++ after the fact, it must be initialize when you declare it. However the simple way around this is to declare your array, do some stuff, then when you need to initialize if just create a new variable, initialize it, and then copy the new array into the old array... like so:

int grid[5][5];

// blah, blah...

int grid2[5][5] = {0,1,2,3,4,5,6,7, ... } // etc.

memcpy(grid, grid2, sizeof(grid));

Simple as that!