AnsweredAssumed Answered

Array of arrays of string constants - help, my brain is melting!

Question asked by FridgeFreezer on Oct 20, 2010
Latest reply on Oct 26, 2010 by FridgeFreezer

Setup is CW7.2, targetting Coldfire V2 MCF52259 micro.

 

I've been trying to work this out but my brain is now well and truly melted. :smileyindifferent:

 

In the project I'm working on, we need to store a lot of (constant) message strings to display to the user, ideally by reference:

messages[MSG_HELLO_WORLD] points to a string "Hello world", but if we later update the messages file so that messages[MSG_HELLO_WORLD] = "Hi, world!" we don't need to change every reference in the code to MSG_HELLO_WORLD to output the updated string.

 

To complicate things, we have several different languages which we need to reference based on the language the user has set, so ideally we'd be able to do this sort of thing:

printf(messages[lang][MSG_HELLO_WORLD]);

 

We need to store this lot as constant global data (so it lives in the micro's flash as .rodata), and define it somehow, but at that point there seems to be so many ways to do this (or so many different names for the same thing) that I can't work out how to go about it;

 

My initial thoughts are to do something like this:

 

messages.h

 

extern const char *msg[NUM_LANGUAGES][NUM_MESSAGES];

 

 

messages.c

enum   
{   LANGUAGE_ENGLISH,
    LANGUAGE_FRENCH,
    NUM_LANGUAGES };

const char *msg[][] = {{[MSG_HELLO_WORLD] = "Hello, world", [MSG_GOODBYE] = "Goodbye"} , {[MSG_HELLO_WORLD] = "Bonjour", [MSG_GOODBYE] = "Au revoir"}};

void say_hello(void)
{
   printf(msg[LANGUAGE_ENGLISH][MSG_HELLO_WORLD]); // Should print "Hello, world"
   printf(msg[LANGUAGE_FRENCH][MSG_HELLO_WORLD]); // Should print "Bonjour"
}

 

Does this seem like a sensible/correct way to go about things?

Outcomes