Goal: In a C module that manages display of text, I'd like to store the pointer to an array of strings, so that I can feed the module different arrays to display. I don't want to copy the array, just the pointer (it is an embedded system with little memory and the array could be long) and I don't want to pass the array to the display function on every call (it is an embedded system with limited hp and the calls could be frequent).
In its simplest form this module looks like:
static uint8_t arrayLen;
static const char * const arrayOfStrings;
void initText(char * const strArray, uint8_t len) {
arrayOfStrings = strArray;
arrayLen = len;
}
void showText(uint8_t lineFrom, uint8_t lineTo) {
for (uint8_t i = lineFrom; i <= lineTo; i++)
displayLine(i, arrayOfStrings[i]);
}
Which is called like so:
const char * const arrayStr1[] = {"ABC", "DEF", "GHI"};
const char * const arrayStr2[] = {"JKL", "MNO", "PQR", "STU"};
...
const char * const arrayStrn[] = {"UVW", "XYZ"};
initText(arrayStr2, 4);
showText(1,2);
...
showText(2,3);
Problem:
if I define arrayOfStrings in the module as const char * const I (obviously) get a compile error cannot assign to variable 'arrayOfStrings' with const-qualified type 'const char * const', but if I change it to any other (non-const) variation, I get no known conversion from 'const char *const[4]' to 'const char ** for 1st argument or some variant thereof, which also makes sense.
I have tried all sorts of variations, but it feels like I am trying to do something that is not possible in C this way. I apparently lack basic understanding to be sure about that.
Searching did not turn up any relevant info.
So my 2 questions are:
- Is it possible at all to pass a pointer to an array of strings to a module function and store it there for later use?
- If so, how should I do it?