2

I have the following;

const CHAR string_1[] PROGMEM = "String 1";
const CHAR string_2[] PROGMEM = "String 2";
const CHAR string_3[] PROGMEM = "String 3";
const CHAR string_4[] PROGMEM = "String 4";
const CHAR string_5[] PROGMEM = "String 5";

const CHAR *string_table[] PROGMEM  = 
{
    string_1,
    string_2,
    string_3,
    string_4,
    string_5
};

How would I save this address of string_table so I could call it in a function;

CHAR acBuffer[20];
UCHAR ucSelectedString = 2; // get string number 3
//
    pcStringTable = string_table ...?? What is the proper line here??
//
strcpy_P(acBuffer, (char*)pgm_read_byte(&(pcStringTable[ucSelectedString])))

Based on the comments below, I changed by structure too;

typedef struct
{
...
CHAR **pasOptions;

I then tried to assign string_table to it;

stMenuBar.pasOptions = string_table;

The compiler throws this warning;

warning: assignment from incompatible pointer type

Any more thoughts?

2
  • @MahmoudAl-Qudsi I don't follow. Haven't I declared it in the struct? Commented May 4, 2012 at 6:55
  • I missed the fact that stmenubar is your unnamed struct. Commented May 4, 2012 at 7:05

1 Answer 1

3

string_table is an array of pointers to strings. An array can decay to a (one-dimensional, because that's the only kind) pointer just fine.

So an array to arrays of strings can be represented as a pointer [think: array] to (pointers of chars [think: strings]).

const char **pcStringTable = string_table;

Which you can then access as any other one-dimensional array:

printf("%s", pcStringTable[2]);
Sign up to request clarification or add additional context in comments.

5 Comments

The code in the OPs example is not a two-dimensional array, it is an array of pointers, which can decay into a pointer-to-pointer. When you downcast a pointer-to-pointer into a single pointer, you end up with a single pointer.
@MahmoudAl-Qudsi I tried your suggestions, but to no avail. I have edited my original question.
That's just a const correctness warning. Since your strings are defined as const char[], the string table variable must be const char ** - I've fixed my answer to reflect that.
Thanks - that did it. Is that even necessary when I declared them as PROGMEM?
That's an arduino-specific compiler command, it tells the compiler to store the variables in the flash instead of the main memory. It has nothing to do with the C code.

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.