Mehod String::toCharArray()
Posted: Wed Feb 10, 2021 2:12 pm
I got surprised with the behaviour of this method.
I had a string object of length 20. I mean, 20 characters plus the terminal null.
After executing the statement
S.toCharArray(buf, 20)
I ended with a buffer containing the first 19 characters of my string, plus the terminating null.
I acknowledge that the reference document of Arduino.cc says:
===========================
string.toCharArray(buf, len)
Parameters
string: a variable of type String
buf: the buffer to copy the characters into (char [])
len: the size of the buffer (unsigned int)
===========================
So if len is 20, this can mean that the total length of the buffer is 20, including the terminating null. At the least, this is a bit misleading.
I had a string object of length 20. I mean, 20 characters plus the terminal null.
After executing the statement
S.toCharArray(buf, 20)
I ended with a buffer containing the first 19 characters of my string, plus the terminating null.
I acknowledge that the reference document of Arduino.cc says:
===========================
string.toCharArray(buf, len)
Parameters
string: a variable of type String
buf: the buffer to copy the characters into (char [])
len: the size of the buffer (unsigned int)
===========================
So if len is 20, this can mean that the total length of the buffer is 20, including the terminating null. At the least, this is a bit misleading.