reinitializing an array?

It’s the small things sometime…

I am using an array declared at the start of the function to store chars from until I reach the specified control char.

char collector[4];

I process that input and then attempt to reinitialize the array to clear out old values:


Doesn’t work - -previous values remain. I resorted to walking through the array (below) but that doesn’t seem right- though it works. Is there another way to do it?
–tx Roy

for(int i = 0; i < 4; i++){
   collector[i] = ' ';

If you're declaring it at the top of a function, the storage class is automatic, you're not initialising it at all, and it is dangerous to assume any contents.


Does absolutely nothing useful apart from evaluating the non-existent fifth element of the array "collector", and then discarding the result of the evaluation.

Can you post the rest of your code to see how best to help you?

I resorted to walking through the array (below) but that doesn't seem right

If you are using the array as a string, it is not necessary to clear every position. Strings are null terminated, so

collector[0] = '\0';

is all that is needed.

After inserting each character, move the NULL along:

collector[i] =;
collector[i] = '\0';

thanks for the comments. I guess it makes sense that my line: collector[4] doesn’t do anything since there isn’t any assignment but it didn’t throw an error either. I did try redeclaring the array with null values

collector = {'/0','/0','/0','/0'}

but that didn’t work. Hadn’t occurred to me to step a null char through the array. Anyway, I was confused @ there not being a way to just blast out the existing values and reset the array to all null values.

fwiw - I’m using that array and atoi (getColor() function) to read in numbers in the range of 0…1024. Tried using bitshifting an bit OR but couldn’t get it to work.

//// Serial Test: receiving large numbers
int RGB[3];
char state = 'i';

void setup() {
void loop(){

  switch (state){
  case 'i':

  case 'c':
    state = 'i';

    /// use new color
    Serial.print("  ");
    Serial.print("  ");
    Serial.println("  ");


int getColor(){
  if  (Serial.available()){
    int position = 0;
    char collector[4];
    char c;
    int counter = 0;
      if (Serial.available()){
        c =;
        if (c == ',' || c =='*'){
          int n = atoi(collector);   //convert to number

          RGB[position] = n;         //store
          position++;                //increment array index

          if (c == '*') {
            state = 'c';
          for(int i = 0; i < 4; i++){
            collector[i] = ' ';

          counter = 0;               //reset char array index
          collector[counter] = c;

I'm using that array and atoi (getColor() function) to read in numbers in the range of 0..1024

Then it should be declared as five elements long, not four - four digits plus a terminating null.

collector[4] doesn't do anything since there isn't any assignment but it didn't throw an error either.

Nor should it - it is syntactically correct.

Wise words:

With most programming languages, it is possible to shoot yourself in the foot, but 'C' hands you the gun.

You may also want to have another look at "getColor" - it should be of type "void"

I resorted to walking through the array (below) but that doesn’t seem right

This is essentially the only way to do it, though of course it will look prettier if you write a function to do it, or use one of the standard ones.

See memset(), bzero(), etc (probably in avrlib rather than the arduino documentation. memset() is part of the C standard…

Note that even if the arduino language accepted a syntax like “collector- = ’ ';” (just to pick something random), it would be executing the same sort of loop anyway, and it wouldn’t be any faster…