In a program, I have a lot of arrays of different length strings, and each array is declared as an array of pointers to those strings, like:
static const char * num_tab[] = {"First", "Second", "Third"};
static const char * day_tab[] = {"Sunday", "Monday", "Tuesday"};
static const char * random_tab[] = {"Strings and arrays can have", "diferent", "lenghts"};
The (pointer to) strings are returned from simple functions such as:
const char * dayName(int index) {
return day_tab[index];
}
Under the AVR architecture, those strings need to be stored in program memory. I understand that the functions need to be changed in order to work also on AVR’s, (they need to copy the string from program memory to a buffer in ram, and return a pointer to that instead).
How can I change the arrays’ initialization to use PROGMEM, without the need to name each individual string?
The only way I found is to define each string with a name (and PROGMEM), and define an array of pointers initialized with pointers to those strings:
static const char d1[] PROGMEM = "First";
static const char d2[] PROGMEM = "Second";
static const char d3[] PROGMEM = "Third";
const char * const day_tab[] = {d1, d2, d3}; // only needs PROGMEM for large arrays
This works, but for large arrays of different sizes, it changes the code from a few lines to hundreds, which makes maintenaince practically imposible. Also, adding or removing a value from an array, will need a renumbering of all of the following items.
Robert Wallner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.