String toCharArray returns weird fault code

I've a simple sketch which should convert a basic string into a char Array. EasyPeasy Lemon pie and thx god or StarTrek that arduino support the string.toCharArray() method.

However, I receive weird error and sit here with no clue what is wrong.

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


String  input = "TEST";
int input_length = input.length()+1;
char splitter[input_length];
input.toCharArray(splitter, input_length);

void loop()
{
 
  if(Serial.available() > 0)
    {
      input   = Serial.readString();
      input.toUpperCase();
    }
     
     Serial.println(input);
     delay(500);
   
}

Error Code

sketch_dec05b:10: error: array bound is not an integer constant before ']' token
 char splitter[input_length];
                            ^
sketch_dec05b:11: error: 'input' does not name a type
 input.toCharArray(splitter, input_length);
 ^
exit status 1
array bound is not an integer constant before ']' token

So what the heck did I wrong ???

Well what you did wrong is using the String class for something so basic so you are punished :slight_smile:

Besides that try declaring

[color=red]const[/color] String  input = "TEST";
[color=red]const[/color] int input_length = input.length()+1;

If you insist using the String class you might want to look at the c_str() function (this gives direct access to the internal String buffer and should be used with care. In particular, you should never modify the string through the pointer returned)

If you insist using the String class you

can forget getting help here!

Found the mistake...was not placed inside the loop :grin: