Assigning values to all elements of an array

I was trying to set the RGB values in a piece of code last night and discovered I couldn't - at least not the way I was trying to do it.

What I was trying to do is this:

byte rgbLED[3] = {0,0,0};


rgbLED[] = {128,255,0};
analogWrite(0, rgbLED[0]);
analogWrite(1, rgbLED[1]);
analogWrite(2, rgbLED[2]);

I tried varying the syntax a little, but to no avail.

Is it possible to accomplish what I'm trying to do? Or do I have to assign each element individually? For a three element array, it's not too big a deal. But if I had 79 elements, it could get real repetitive.


Aren't you initiating the array wrong? You initiate it with rgbLED[3] but then try to call back to it with just rbgLED[]. There is no need to have the 3 inside the [].

The 1st statement, the declaration and initialization is correct. After that initialization, the array elements must be assigned individually.

I agree with PaulS...

the array elements must be assigned individually

but only partially. If you understand pointers, you can copy the array from another array with memcpy in one call.

The amount of time you save is probably negligible, and is a little more advanced and prone to error if the array sizes change as an example.

A for loop is probably your best bet.

What if I were to re-initialize the array each time I want to set its values? Would that cause more overhead than it's worth?

byte rgbLED[3] = {analogRead(0),analogRead(1),analogRead(2)};


That code will work, but not by re-initializing an array, but by initializing a new array each time through loop (assuming that code is in loop).