Do I need to reset variables to 0 ?

Hello,

I wrote a string split function witch splits a string which contain integers separated by semicolons into separate integers. The string contains 24 integers so I need to make it 24 times. Here is an example of the 2 first integers split :

      semicolunPosition = trimIncomingString.indexOf(';');
                  integer1String = trimIncomingString.substring(0, semicolunPosition);
                  trimIncomingString = trimIncomingString.substring(semicolunPosition+1, trimIncomingString.length());
                  semicolunPosition = 0;
                  integer1 = integer1String.toInt();
    
                  semicolunPosition = trimIncomingString.indexOf(';');
                  integer2String = trimIncomingString.substring(0, semicolunPosition);
                  trimIncomingString = trimIncomingString.substring(semicolunPosition+1, trimIncomingString.length());
                  semicolunPosition = 0;
                  integer2 = integer2String.toInt();

My question is : Do I need to reset semicolunPosition to 0 after each integer and reset the trimIncomingString to "" at the end of the function for a new loop or will they reset by semselves after new assignment ?

No, you just need to set it to something useful before its reused. Which you do.

semicolunPosition = trimIncomingString.indexOf(';');

We can’t see where trimIncomingString is populated, so it might if it is populated using ‘+=’ and you do not want the old data. If there is only a simple assignment, then it is replaced.

Why do you ask, some debugging problem?

My trimIncomingString is obtained by assignement. So, if I well understand, only variables obtained by += have to be cleared for new usage ? Variables obtained by assignement are automatically reset when a new assignement is done ? Can you confirm ?

I need to simplify my sketch because I have to care about the size of it because it is a very big sketch and the Uno memory is limited.

Thank you very much

JP744:
Variables obtained by assignement are automatically reset when a new assignement is done ? Can you confirm?

loose the notion of reset, a String can be assigned an empty string, and an int can be assigned zero, if this is what you need. If you need it to have real data, then just assign that to it.

JP744:
if I well understand, only variables obtained by += have to be cleared for new usage ?

The += operator is one example. I used it as the String library overloads it. If you have control of the code, just change the ‘+=’ to ‘=’. Do you understand the differences between the two?

I need to simplify my sketch because I have to care about the size of it because it is a very big sketch and the Uno memory is limited.

Then you should probably stick to character arrays instead of the String library. To a novice C++ programmer the memory usage can seem quite unpredictable when using Strings.

What's wrong with using strtok()?

Thank you. I don't have the knowledge of C++ to use strtok() I tried before but cannot make the string split.

This is where I learned about strtok().

...R