How can I pass a character array as a function parameter?
I've studied for several days and can't seem to figure it out.
My intent is to have a list of "Messages" stored in program memory and push them to the display based on various conditions.
The output as written looks to be panic-type error or overflow.
Test code:
const char v_0[] PROGMEM = {"ABCdef"};
const char* const v_table[] PROGMEM = {v_0};
char cBuffer[30];
void setup()
{
Serial.begin(9600);
while(!Serial){delay(10);}
fx(v_table[0]);
}
void loop(){}
void fx(const char *msg)
{
Serial.print("cBuffer: ");
strcpy_P(cBuffer, (char*)pgm_read_word(&(v_table[0])));
Serial.println(cBuffer);
/*
Trying to pass the character array through the function
Serial.print("cBuffer post fx: ");
strcpy(cBuffer, (char*)pgm_read_word(*msg));
Serial.println(cBuffer);
*/
}
gfvalvo
December 31, 2018, 9:30pm
2
#include "Arduino.h"
const char aString[] PROGMEM = "Hello World";
const __FlashStringHelper *strPtr = reinterpret_cast<const __FlashStringHelper *>(aString);
void stringFunct(const __FlashStringHelper *ptr);
void setup() {
Serial.begin(115200);
delay(1000);
stringFunct(strPtr);
}
void loop() {
}
void stringFunct(const __FlashStringHelper *ptr) {
Serial.println(ptr);
}
gfvalvo
December 31, 2018, 10:44pm
3
For an array of strings in PROGMEM:
#include "Arduino.h"
const char *stringArray[] PROGMEM = {"Hello World", "Foo", "Bar", "Test"};
const uint8_t numStrings = sizeof(stringArray) / sizeof(stringArray[0]);
const __FlashStringHelper **strPtr = reinterpret_cast<const __FlashStringHelper **>(stringArray);
void stringFunct(const __FlashStringHelper *ptr);
void setup() {
Serial.begin(115200);
delay(1000);
for (uint8_t i = 0; i < numStrings; i++) {
stringFunct(strPtr[i]);
}
}
void loop() {
}
void stringFunct(const __FlashStringHelper *ptr) {
Serial.println(ptr);
}
Thank you - I'll give it a try tomorrow.
It'll take me some time to read through to understand why and how.
hoffmajf:
How can I pass a character array as a function parameter?
void func(char functionParameter[]){}
or the equivalent:void func(char *functionParameter){}
The hard part is that the array dimensions and size are not known in the function. If the dimensions are compile-time constants you can specify them in the parameter list:void func(char functionParameter[3][5]){}
hoffmajf:
My intent is to have a list of "Messages" stored in program memory and push them to the display based on various conditions.
Data in PROGMEM brings its own set of problems. The compiler doesn't keep track of which addresses point to RAM and which point to PROGMEM so it is entirely up to the program author to use the PROGMEM fetch functions on PROGMEM pointers and not on RAM pointers.
Thank you all for your assistance.
I admit I'm a hack at this, but enjoy it never-the-less.
This test sample works:
const char v_0[] PROGMEM = "ABCdef";
const char v_1[] PROGMEM = "ZYXwvu";
PGM_P const v_table[] PROGMEM = {v_0, v_1};
char cBuffer[30];
void fx(PGM_P const v_table[], uint8_t Element)
{
Serial.print("cBuffer: ");
strcpy_P(cBuffer, (PGM_P)pgm_read_word(&(v_table[Element])));
Serial.println(cBuffer);
}
void setup()
{
Serial.begin(115200);
while(!Serial){delay(10);}
fx(v_table, 0);
fx(v_table, 1);
}
void loop(){}
Output:
cBuffer: ABCdef
cBuffer: ZYXwvu