Initializer for an array of pointers to arrays of strings

Discussion in 'C Programming' started by Steve, Jan 24, 2004.

  1. Steve

    Steve Guest

    I want an initializer for an array of pointers to arrays of strings.

    So I can do something like this:

    const char *t1[] = { "a", "b", "c", NULL };
    const char *t2[] = { "p", "q", NULL };
    const char *t3[] = { "w", "x", "y", "z", NULL };
    const char **test[] = { t1, t2, t3, NULL };

    I was wondering whether the is a more elegant way of writing such an
    initializer, one that does away with all the redundant names t1, t2, etc.

    For a 2d array of strings I could do:

    const char *test2[][5] =
    {
    { "a", "b", "c", NULL, NULL },
    { "p", "q", NULL, NULL, NULL },
    { "w", "x", "y", "z", NULL }
    };

    Is there something similar for arrays of pointers to arrays of strings?
     
    Steve, Jan 24, 2004
    #1
    1. Advertising

  2. Steve wrote:

    > I want an initializer for an array of pointers to arrays of strings.
    >
    > So I can do something like this:
    >
    > const char *t1[] = { "a", "b", "c", NULL };
    > const char *t2[] = { "p", "q", NULL };
    > const char *t3[] = { "w", "x", "y", "z", NULL };
    > const char **test[] = { t1, t2, t3, NULL };
    >
    > I was wondering whether the is a more elegant way of writing such an
    > initializer, one that does away with all the redundant names t1, t2, etc.
    >
    > For a 2d array of strings I could do:
    >
    > const char *test2[][5] =
    > {
    > { "a", "b", "c", NULL, NULL },
    > { "p", "q", NULL, NULL, NULL },
    > { "w", "x", "y", "z", NULL }
    > };
    >
    > Is there something similar for arrays of pointers to arrays of strings?


    Will this do?

    int main(void)
    {
    const char **test[] = { (const char *[]) {"a", "b", "c", 0},
    (const char *[]) {"p", "q", 0},
    (const char *[]) {"w", "x", "y", "z", 0},
    0 };
    return 0;
    }




    --
    Martin Ambuhl
     
    Martin Ambuhl, Jan 24, 2004
    #2
    1. Advertising

  3. Steve

    Steve Guest

    Martin Ambuhl wrote:
    > Will this do?
    >
    > int main(void)
    > {
    > const char **test[] = { (const char *[]) {"a", "b", "c", 0},
    > (const char *[]) {"p", "q", 0},
    > (const char *[]) {"w", "x", "y", "z", 0},
    > 0 };
    > return 0;


    Yes, thanks, that does do the trick.

    I notice that my C compiler is ok with it, but my C++ one not. Curious, but
    that would be a question for another group.

    Thanks again,
    Steve
     
    Steve, Jan 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    1
    Views:
    382
    Victor Bazarov
    Jan 25, 2004
  2. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    396
  3. Replies:
    1
    Views:
    672
    -berlin.de
    Mar 28, 2005
  4. Francesco
    Replies:
    2
    Views:
    1,148
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    710
Loading...

Share This Page