char table in a function retains values between calls without static [SOLVED]

Hi,

If you take a look at the code below, it works perfectly, even if buff is not declared static.

Why?

bool table[3][3] = {false, false, false,
                    false, false, false,
                    false, false, false};

bool readMonitor() {
  char buf[3];
  static byte i = 0;
  if (Serial.available()>0){ buf[i] = Serial.read(); i++; }
  if (int(buf[i-1]) == 13) {
    i = 0;
    table[buf[0]-48][buf[1]-48] = true;
    return true;
  }
  return false;
}


void setup() {
  Serial.begin(9600);
}

void loop() {
  if (readMonitor()) {
    for (int i = 0 ; i < 3 ; i++) {
      for (int j = 0 ; j < 3 ; j++) Serial.print(table[i][j]);
      Serial.println();
    }
    Serial.println();
  }
}

You got lucky.

Ever hear the phrase "works like a charm" ?

How often do charms work?
Would you rely on that?

The stack locations where buff is stored just don't happen to be overwritten by other code.

So it is placed on the stack.

Thanks it makes sense,

Jacques