Having an issue with strncat(). It doesn't seem to be doing what its supposed to. currWord never gets populated (line 66), at least not with character data. I have a similar program that uses it just fine.
This is reading in a file, parsing it, and trying to load it into an array.
this is the output (it wouldn't all paste in for some reason. currWord seems to have non character data that won't copy/paste. I can see the squares in the serial monitor.
"currChar" is not a C-string, as the function str(n)cat requires (it expects a pointer, not a value, as the source string argument).
This works:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char currWord[10] = {0};
char currChar[] = "a";
strncat(currWord, currChar, 1);
Serial.print(currChar);
Serial.print('-');
Serial.println(currWord);
}
void loop() {
// put your main code here, to run repeatedly:
}
Or this:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char currWord[10] = {0};
char currChar = 'a';
strncat(currWord, &currChar, 1);
Serial.print(currChar);
Serial.print('-');
Serial.println(currWord);
}
void loop() {
// put your main code here, to run repeatedly:
}
Thanks for the response.
to make sure I understand...
this
char currChar[] = "a";
makes currChar a pointer because the double quotes makes a String and setting it = is really establishing a pointer to it, even though currChar wasn't defined as a pointer? Why does it need to be declared as an array?
char currChar = 'a'; is setting a single character variable which is holding only the character a, and then has to be dereferenced as &currChar to supply its address to the function?
OF course I am reading in 1 char at a time via the .read method of File so I assume the 2nd example is the more appropriate one to use.
not the same as passing a c-string which is an array of chars terminated by a Nul '\0'.
a more conventional approach is to read a buffer, the read returns the number of chars read, whether the size of the buffer, something less or 0 at EOF.
chars can then be processed as desired, one at a time or in some blocks possibly delimited by a termination char (e.g. \n)