This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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

  • 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

  • Yes, but!

    In my application, I did an independant command interpreter, single char commands. And I feed him with a structure, containing all needed information to do his job.

    struct Menu{
      // String with allowed chars
      char code *commands;
      // count of commands
      unsigned char count;
      // pointer to the array with
      // strings to display
      char code **display;
      // count of strings
      unsigned char lines;
      // pointer to an array with
      // pointers to the needed functions
      void (code **dest)(void);
    };
    

    this makes it easy to switch between menus and I display the appropriate strings like this

    // dpstring serves a 4x20 char display
    
    dpstring(col, row, (const char code *)Menu->display[select]);
    

    what I want to do, is

    dpstring(col,row,(const char code *)Menu->display[lang][select]);
    

    and therefor, I think, I need either a proper initialized 3dimensional array or, better, an array of pointers to arrays of pointers to strings, like

    // german menu
    code const char code *MenuG[]={
      "Punkt eins",
      "Punkt zwei",
      "Punkt drei"
    };
    // english menu
    code const char code *MenuE[]={
      "point one",
      "point two",
      "point three"
    };
    // independant menu pointer
    // something like
    code const char code *Menu[]={
      MenuG,
      MenuE
    };
    // and then
    struct Menu root={
      "123\x1b",     // command chars
      3,
      Menu,          // strings
      2,
      Rootfunctions  // functions
    };
    
    the interpreter uses a pointer to such a structure so I switch between menues, using an assignment. And I would like to switch between languages, assigning a value, like
    lang = deutsch;
    // or
    lang = english;
    
    single language version works perfect. But if I try to go multilingual, the best result, I reach, is a scrambled screen.

    Another exercise?

    - Eckhard

  • Your variable Menu has a bad declaration. Try:

    #include <stdio.h> 
    #include <reg52.h>
    
    typedef char     code* MenuItem;
    typedef MenuItem code* Menu;
    typedef Menu     code* Menus;
    
    typedef struct MenuCtrl_Tag{
      char code*    commands;
      unsigned char count;
      Menus         menus;
      unsigned char lines;
      void (code **dest)(void);
    } MenuCtrl;
    
    // german menu
    code MenuItem menuG[]={
      "Punkt eins",
      "Punkt zwei",
      "Punkt drei"
    };
    // english menu
    code MenuItem menuE[]={
      "point one",
      "point two",
      "point three"
    };
    // independant menu pointer
    // something like
    code Menu menusGE[]={
      &menuG,
      &menuE
    };
    
    code MenuCtrl root={
      "123\x1b",     // command chars
      3,
      &menusGE,       // strings
      2,
      NULL           // functions for now
    };
    
    void main()
    {
      TI = 1;                              //to make printf works in debugger
      printf( "%s\n", root.menus[1][1] );  //should be "point two"
      while( 1 ) ;
    }
    <\pre>