Very strange syntax?

Found a piece of code, working fine. But I do not fully understand.

do {
  inString[inCount] = Serial.read();     
  if (inString [inCount] == 13) break;{
    (++inCount < 20);
    inString[inCount] = 0;  // null terminate the string  
  }
}
while (Serial.available());           

If the variable == 13 then the do-while loop will exit.
But why the { } after the break? Does it belong to the if? Skips the else?
What is (++inCount < 20);? Another if condition?

Thanks for clarification :wink:

Pete

I don't know if you just copied and typed in by yourself, but IMHO it's wrong.
That curly brackets are irrilevant when used that way, the "(++inCount < 20);" doesn't have any sense (except for incrementing that variable) and an "if()" is missing.
Thus, I think it should read like:

do {
  inString[inCount] = Serial.read();     
  if (inString [inCount] == 13) break;
  if (++inCount < 20) {
    inString[inCount] = 0;  // null terminate the string  
  }
}
while (Serial.available());     

That "if (++inCount < 20)" is needed to correctly end the received string (I suppose the variable is defined as "char inString[20];").

1 Like

Isn't that reading from the serial port before one knows if there is any thing in the serial buffer (available())?

Serial input basics tutorial.

In both versions the loop is unbounded meaning inString can be overrun.

They have no practical effect. Just a code block. The coder probably made a mistake, and it would be wise to look elsewhere for examples.

Yep, I hope that while loop is inside an outer "if (Serial.available())". I'd better put the while test before:

while (Serial.available())
{
  inString[inCount] = Serial.read();     
  if (inString [inCount] == 13) break;
  if (++inCount < 20) {
    inString[inCount] = 0;  // null terminate the string  
  }
}   

Then, there's also a weird fact, if a '\r' (13) is received, the string terminator is missing (while the '\r' character will be part of the buffer). And what if a '\n' is received after the '\r'?
That's not really part of the OP question, but I'd use a different approach, like:

inCount = 0;
while (Serial.available()) {
  char c = Serial.read();
  // If is LF or max buffer length reached, then exit
  if ( c == '\n' || inCount == 20)  break;
  // If not CR and the buffer is not full, add the character
  if ( c != '\r' && inCount < 20 ) {
    inString[inCount++] = c;
    inString[inCount] = 0; // Always terminate the string!
  }
}

Given this...

...and more than 20 characters received without a terminating character your code overruns the buffer when it stores the null.

Yep, but that was just my guess (maybe "char inString[21];" you're right) not knowing the complete code. Obviously on my code (the last one on post #6) just replace 20 with the buffer length+1 whatever it is, and it won't overrun :wink:

Wouldn't inString be '0000000000000000000' plus however many characters were received until '\r' ?

Thank you all for your great and quick reply!!!
Obviously the code was wrong but the compiler is generous. So no secret C++ features found.

Thanks :grinning:

No, because of the "++inCount" you probably missed from that code. The '\0' (not '0') will always be added after the received character.
Obviously my code is just a snippet, as no full code has been published, so somewhere in the code should be a "inCount = 0;" statement to "reset" the input characters counter to zero, followed by a "inString[0] = 0;" to properly set the initial "empty string" waiting for the next data.
Anyway, this is the best solution IMO, where I added just the buffer reset before the while:

inCount = 0;
inString[inCount] = 0; // I forgot this!
while (Serial.available()) {
  char c = Serial.read();
  // If is LF or max buffer length reached, then exit
  if ( c == '\n' || inCount == 20)  break;
  // If not CR and the buffer is not full, add the character
  if ( c != '\r' && inCount < 20 ) {
    inString[inCount++] = c;
    inString[inCount] = 0; // Always terminate the string!
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.