2

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:

  1. 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?
  2. If so, how should I do it?

1 Answer 1

1

The type of the argument expression

arrayStr2

is const char *const * due to the implicit conversion of the array designator to pointer to the array element type..

So the variable arrayOfStrings should be declared like

static const char *const * arrayOfStrings;

And the functions also should be declared like for example

void initText( const char * const * strArray, uint8_t len );
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Vlad! As I read your reply it makes sense, but I could not think of it myself...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.