I'm having an error in buffers that handle strings and I can not figure out what the problem is. The example below shows what is happening. Can someone help me?
void setup() {
char* ss1 = "";
char* ss2 = "";
String txt = "";
Serial.begin(115200);
txt = "1234560001a";
txt.toCharArray(ss1, txt.length());
Serial.printf("\n\n");
Serial.printf("ss1 = %s\n", ss1); // It prints "1234560001" whithout last char "a"
// Force incorrectly "ss1" to be equal to "ss2" and still removes the last character.
txt = "abcdef";
txt.toCharArray(ss2, txt.length());
Serial.printf("ss1 = %s\n", ss1); // It prints "abcde" whithout last char "f"
Serial.printf("ss2 = %s\n", ss2); // It prints "abcde" too
}
You need to Google Arduino printf (which I just did) and look at all the articles on the proper use of printf and more importantly sprintf (which I just did) and then you will know what you need to do to fix your code. Enjoy your reading.
No.The problem is not the printed value. The problem is the confusion on pointers that is occurring when copying the contents of the last one variable, where its content overwrites the contents of the previous variable. Also, copying the contents of the "String" variable to the "char *" variable, is also removing the last character.
thanks
No, according to the documentation it is sufficient to have a pointer type buffer and the memory length will be allocated by the "toCharArray()" function. In addition the '\0' is also appended by the "toCharArray()" function, so that, for example:
String txt = "abc";
char ss1 [40];
txt.toCharArray (ss1, txt.length());
ss1 should be "abc", but it is just becoming "ab".
The second problem is related to the overlapping of the contents of the variables, which apparently there is a problem of pointers that I did not find.
wBB:
No, according to the documentation it is sufficient to have a pointer type buffer and the memory length will be allocated by the "toCharArray()" function. In addition the '\0' is also appended by the "toCharArray()" function, so that, for example:
String txt = "abc";
char ss1 [40];
txt.toCharArray (ss1, txt.length());
ss1 should be "abc", but it is just becoming "ab".
The second problem is related to the overlapping of the contents of the variables, which apparently there is a problem of pointers that I did not find.
You're right. The documentation says nothing, from which one I can conclude that if it were necessary to inform the buffer size and also the '\ 0', it would be written in the documentation. As it is not written, it means that the "toCharArray" function takes care of everything. Anyway, I've tested your suggestion, I created a buffer large enough and I've include the "\ 0" at the end. But it still doesn't work.
Thanks.
sketch_jul08a:11: error: 'class Serial_' has no member named 'printf'
Serial.printf("\n\n");
It could be useful to post which board you're using; definitely not an AVR based board.
The '\0' is not necessary. This is the function in WString.cpp that the toCharArray() method calls; the 3rd argument is 0.
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
{
if (!bufsize || !buf) return;
if (index >= len) {
buf[0] = 0;
return;
}
unsigned int n = bufsize - 1;
if (n > len - index) n = len - index;
strncpy((char *)buf, buffer + index, n);
buf[n] = 0;
}
It does add the terminating nul character.
Your ss1 and ss2 are pointers to buffers, not buffers; for all purposes, this does not make much of a difference. But ss1 and ss2 each point to a buffer with a size of one byte. The reference clearly states that the len argument is the size of the buffer.