Array error: expected constructor, destructor,...

Hi together!

Another quick newbie question... :slight_smile:
Why does that code not work?

int myValue[10];
myValue[2] = 0;

void setup()
{
}

void loop()
{
}

I always get the error

error: expected constructor, destructor, or type conversion before '=' token

Whats wrong here??

Thanks a lot,
Sebastian

Ah, I think, I got it.
Should be

int myValue[10];


void setup()
{
myValue[2] = 0;
}

void loop()
{
}

Right? Too late to program at all.... :wink:

Right. You can't have code that's not inside some function, and you can't have an initialized that sets only one element of the array. You could also have done:

int myValue[10] = {0,0,0,0,0, 0,0,0,0,0};

Also, C will initialize arrays to all zeros automatically (though I don't think it's good form to count on this!)

Strangely,

int myValue[10] = {0};

also initialises the whole of "myValue" to zero.