So far I have managed to studiously avoid anything to do with strings but I now want to send a data string to serial.
I am using the dtostrf command and most straightforward background I could find is at
with a worked example at
from which i got up some test code for three DS18B20s but currently using dummies. This actually works but I don't understand why. I don't understand the final argument for dtostrf, which defines the size of the array. I would never get more than seven characters from the float but it appears that I must define the array with at least eight.
Th code below delivers as required, but if I have the buffers as
char stroutx[7];
I get the third value combined with the second, and also in its proper place i.e.
-10.07
-123.4510.05
10.05
but if I reduce the second dummy to -12.45 it all comes out OK.
How many characters are need to hold the value -123.45 as a string? Then add one for the terminating NULL, and tell me how 7 is enough spaces in the array.
Thanks. I can actually count as well as you can but I have never heard of terminating nulls. All I had seen was an admonition to "leave plenty of space", and I thought "strout0[15]" was rather excessive in the light of what I'm doing.
It's the result of the way C strings work. A C string is always terminated with a NULL, which is a zero (not the printable character 0, but rather a byte with no bits on.
What you see is because the strings in your code happen to be stored in contiguous memory locations, so here's what they look like after filling them with the original values The second line in each case shows the index of the char arrays. I'll show the NULL as a @ sign, and unknown values as a *
As if this wasn't a bad enough problem, things get even worse if another type of variable gets clobbered by having too many characters in an array, and you suddenly find yourself jumping out of the way of a projectile that was supposed to be flung Northeast, but the integer holding the direction has just been changed to Southeast.