Looping through a Char array - splitting into seperate Strings - Arduino

Viewed 466

I am having a bit of difficulty with the Arduino project I am currently working on.

The purpose of the function I am developing is to take in a char array variable received via an NRF wireless module and separate this into 2 different string variables. The first 13 characters into one string, and the remaining into another.

void receiveData(char* receivedData){ // Function to place the received data into the correct variable.
  for(int i = 0;i < sizeof(receivedData);i++){
    if(i < 13){
      String variableName = variableName + receivedData[i]; // Builds a string of the variablename.
    }
    else{
      String value = value + receivedData[i]; // Builds a string of the value.
    }   
  } 

I have worked through a few different ways but no luck.

Any help will be greatly appreciated, Thank you!

1 Answers
String variableName = variableName + receivedData[i];

Here you are defining the variable in every iteration of the loop. You should declare the variable before the loop:

String variableName;
for () {
    variableName = whatever;
}

In addition sizeof(receivedData) will only give you size of the pointer, not the size of the string that you probably expect.

Related