I'm trying to change the contents and length of an array based on a condition, and getting some unexpected behavior.
Stripped down code:
void setup() {
Serial.begin(115200); // Allows serial monitor output (check baud rate)
}
void loop() {
bool condition = false;
// this works
int options[4] = {1, 0, -1, -1};
// things get weird if I dynamically build the array
int options2[4];
if (condition) int options2[3] = {1, 0, -1};
else int options2[4] = {1, 0, -1, -1};
Serial.println(options[0]);
// prints "1"
Serial.println(options2[0]);
// prints "20000"
delay(100);
}
In the above, options[0] = 1, which is as expected. However options2[0] = 20000...
I'm guessing the reason is that I'm redeclaring options2, but I don't see how I change it's contents and length without doing so.
Any tips on how I can change the contents of options[], including the number of items, based on a condition?