array of pointers to arrays of pointers to string

Yes, the real problem is, I would like to write an easy to switch, multilingual menu. And I imagine, an array like that should work. But there seems to be some brain blocking in
1. How to define and initialize and
2. How to access finally the wanted string.

Ok, ommitting the string definition, the array of pointers to strings is easy.

char *lang[] = {string1,string2,...};

lang should be the pointer to this array.

If I define an array of such pointers, is it
char *langset[] = {lang1,lang2,...};
or
void *langset[] = {lang1,lang2,...};

and how to access

print(langset[langchoice][stringchoice]);???

Or am I just thinking too much and there is a much easier solution to the prob?

Any ideas appreciated

Parents
  • This is just a C question, try this:

    #include <stdio.h>
    
    #define NUM_OPTS 5
    
    int main(void)
    {
        int idx;
    
        const char * const pMenuEnglish[NUM_OPTS] =
        {
            "1. Option A",
            "2. Option B",
            "3. Option C",
            "4. Option D",
            "Escape"
        };
    
        /* Force other menus to have same entries as English
        */
        const char * const pMenuFrench[NUM_OPTS] =
        {
            "1. Optionne A",
            "2. Optionne B",
            "3. Optionne C",
            "4. Optionne D",
            "Escapez"
        };
    
        /* Language independent pointer
         */
        const char * const *pMenu;
    
        /* Choose language 1
        */
        pMenu = pMenuEnglish;
    
        printf("\nMenu\n");
        for (idx = 0; idx < NUM_OPTS; ++idx)
        {
            printf("  %s\n", pMenu[idx]);
        }
    
        /* Choose language 2
        */
        pMenu = pMenuFrench;
    
        printf("\nMenu\n");
        for (idx = 0; idx < NUM_OPTS; ++idx)
        {
            printf("  %s\n", pMenu[idx]);
        }
    
        return 0;
    }
    Note, you will most likely want to place the language dependent strings into CODE space. I'll leave that as an exercise.

    - Mark

Reply
  • This is just a C question, try this:

    #include <stdio.h>
    
    #define NUM_OPTS 5
    
    int main(void)
    {
        int idx;
    
        const char * const pMenuEnglish[NUM_OPTS] =
        {
            "1. Option A",
            "2. Option B",
            "3. Option C",
            "4. Option D",
            "Escape"
        };
    
        /* Force other menus to have same entries as English
        */
        const char * const pMenuFrench[NUM_OPTS] =
        {
            "1. Optionne A",
            "2. Optionne B",
            "3. Optionne C",
            "4. Optionne D",
            "Escapez"
        };
    
        /* Language independent pointer
         */
        const char * const *pMenu;
    
        /* Choose language 1
        */
        pMenu = pMenuEnglish;
    
        printf("\nMenu\n");
        for (idx = 0; idx < NUM_OPTS; ++idx)
        {
            printf("  %s\n", pMenu[idx]);
        }
    
        /* Choose language 2
        */
        pMenu = pMenuFrench;
    
        printf("\nMenu\n");
        for (idx = 0; idx < NUM_OPTS; ++idx)
        {
            printf("  %s\n", pMenu[idx]);
        }
    
        return 0;
    }
    Note, you will most likely want to place the language dependent strings into CODE space. I'll leave that as an exercise.

    - Mark

Children
More questions in this forum