2

I have the following code:

int count = 0;
char serial_char;
char serial_message[96];


void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available() > 0){

    serial_char = Serial.read();
    if (serial_char == '\n' || serial_char == '\r'){

      serial_message[count] = 0; // <<-- What does this line do?
      count = 0;
      Serial.println(serial_message);

    }
    else{
      serial_message[count++] = serial_char;
    }

  }

}

With this code, I can type a message into the serial monitor, and it will print the message back at me. So if I type 'Goodbye', it returns:

Goodbye

If I send it another message 'Word', it returns:

Word

However, my questions is when I comment out the line:

serial_message[count] = 0;

And then I repeat the same sequence of actions before, 'Goodbye' returns 'Goodbye', but when next I enter 'Word', it returns:

Wordbye

Could anyone explain this to me? I am really curious to know why this occurs. What does adding that 0 in the char array accomplish? Thank you.

1
  • Try sending a 96 character string and a 97 character string so your code tries to read and then write outside its buffer.
    – Dave X
    Commented Mar 5, 2016 at 20:09

2 Answers 2

1

There is a difference between char[] and String.
You are using char[] which is the best way to work on a small processor like the Arduino. However; it is also a bit more difficult.

Char array is just a piece of memory that can contain any data. Because we want to put a variable length string in there we need a convention to know what is the last character of the string. By convention this is a value 0.
This is why we speak about "null terminated strings".

This explains why after sending word (without adding the 0 at the end" you get "Wordbye" because the memory contains 'W' 'o' 'r' 'd' 'b' 'y' 'e' and 0.

The reason why it works OK with 'Goodbye' is because all memory gets set to 0 at startup of the processor.
So as long as the last send word is longer than the previous it works fine.

A last thing. The memory does not actually contain 'W' 'o' 'r' 'd' 'b' 'y' 'e'. The memory contains the ASCII values of the letters 'W' 'o' 'r' 'd' 'b' 'y' 'e'

4

The zero-value, the "null character", indicates the end of the string. For strings, you allocate some maximum space, in your case 96 bytes:

char serial_message[96];

When storing variable-length strings in that, you need to indicate where the string ends. That's what the zero value does in a null-terminated string. (Note that 0 is really binary 0x00 here; it's not the ASCII character 48, 0x30.) So, the 96 bytes allow for a string of at most 95 characters, followed by the terminator. When printing, it prints the memory contents until it finds that null character.

2
  • Oops. I changed it and forgot to change all of it. I have edited it to be correct. Thank you! Commented Mar 5, 2016 at 14:37
  • 2
    Note that the serial_message[96] is initialized with zeros at the beginning, providing default null terminations for strings up to 96 characters long. What might be interesting is what would happen if one sends strings 96 or more characters long. At 96, the Serial.println() wouldn't stop printing until it hits a null in whatever is after the buffer in memory, and if you sent more than 96 characters, it could write over important RAM and crash.
    – Dave X
    Commented Mar 5, 2016 at 20:07

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.