I have a problem and I can't fix it. I'm busy with an java to arduino project.

Java writes a string using writeString(); Arduino reads the serial monitor and put the incoming string into a function.

then I want to put the string into a variable and the variable into a char arr[];

The problem is: When I put the string directly in the arr[]; it works, but when I put first the incoming string in a variable and then the variable in the arr[]; it gives me the next error:

string-parser:44: error: array must be initialized with a brace-enclosed initializer

exit status 1

initializer fails to determine size of 'arr'

this is the code that doesnt work:

input = "2,5>2,6>2,7>4,8";

String splitStringIntoCoordinates(String input){
  char arr[] = input; // Place incoming route in char array

This is the code that does work:

String splitStringIntoCoordinates(String input){
  char arr[] = "2,5>2,6>2,7>4,8"; // Place incoming route in char array

I hope anyone can help me. Thanks!

1 Answer 1


A String isn't a char array. You can't assign one to the other.

You shouldn't be using String, it's too heavyweight, and how you are passing your String object is wrong, it wastes huge amounts of memory making clones.

You should read this.

The only time you should ever consider using a String object is if you are using a third party library that requires a String object - and even then you should consider other methods.

Regardless of that, you need to copy the content of your string, not the string itself. This code should work, but it doesn't address the underlying String issues:

String splitStringIntoCoordinates(String &input){
  char arr[input.length() + 1]; // Create a char array big enough including the terminating NULL
  strcpy(arr, input.c_str()); // Place incoming route in char array
  // ... etc ...
  // return a string

If you just want to reference the String as a C string you can just use the function input.c_str() to get the internal char * array pointer.

  • Thank you majenko! This worked for me. I never tought of that.
    – Dennis
    May 26, 2016 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.