3

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!

3
  • Does this answer your question? C++: sizeof for array length Commented Mar 18, 2020 at 14:24
  • 1
    variableName stays in the scope of your if loop, so once you enter your for loop again, all changes made to variableName will dissapear. You should declare value and variableName outside your for loop (String variablename, value;) Commented Mar 18, 2020 at 14:24
  • 1
    sizeof(receivedData) is the same as sizeof(char*), which will be 4 or 8 depending on the platform (probably 4 on an Arduino). You need to pass the size to the function. Commented Mar 18, 2020 at 14:24

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

3 Comments

I see that, thankyou. The problem I am getting is say I pass the char array of "voltageBefore34", when I serial print and monitor this output of variableName, I only get 'vvo' and I am unsure why.
Can't really answer that, I strongly suggest that you use a debugger and do a step-by-step analysis of what your loop is doing.
I have got it working, Pablo. Thank you very much for your help. much appreciated!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.