mriksman
mriksman

Reputation: 309

Array of pointers to strings

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?

Upvotes: 2

Views: 2349

Answers (1)

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29519

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]);

Upvotes: 3

Related Questions