Trying to save some RAM in a project. The attached code is for demonstrating my problem.
The code can pass "F() strings" to the function but occasionally I also want to be able to pass a regular char array (C string) or convert it appropriately before passing it.
Any kind soul how can help me out?
#include <SoftwareSerial.h>
SoftwareSerial debugSerial(NULL, A1); // RX, TX.
void setup() {
debugSerial.begin(9600);
printToSerial(F("LALA")); // This works
/*
--- I want to be able to do this also i. e. pass a char array to the function ---
char test[6];
memset(test, '\0', sizeof(test));
strncpy(test, "12345", sizeof(test)-1);
printToSerial(test);
*/
}
void loop() {
}
void printToSerial(const __FlashStringHelper* output) {
char buffer[128];
memset(buffer, '\0', sizeof(buffer));
strlcpy_P(buffer, (const char PROGMEM *)output, sizeof(buffer));
debugSerial.println(buffer);
}
Is this a typo?
"strlcpy_P(buffer, (const char*) output, strlen_P(output));"
Shouldn't that line be like in my example to avoid overflowing the buffer if output is to large?
"strlcpy_P(buffer, (const char*) output, sizeof(buffer));"
nollieflip:
Is this a typo?
"strlcpy_P(buffer, (const char*) output, strlen_P(output));"
Shouldn't that line be like in my example to avoid overflowing the buffer if output is to large?
"strlcpy_P(buffer, (const char*) output, sizeof(buffer));"
I use only strcpy_P. one knows the max size of progmem strings. but yes, buffer size is right there