Inline initialization of a struct containing a string array

Discussion in 'C Programming' started by Antti Karanta, Mar 27, 2008.

  1. Hi!

    Is it possible to inline initialize a struct whose one member is a string
    array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
    something like this:


    typedef struct {
    char** x ;
    int y ;
    } Foo ;

    static const Foo myfoos[] = {
    { { "hello", "world", NULL }, 12 },
    { NULL, 0 }
    } ;


    This produces warnings (from mingw gcc) for line w/ "hello" on it:

    "initialization from incompatible pointer type"
    "excess elements in scalar initializer"
    "braces around scalar initializer"

    So obviously this is not the way to do it, or I am missing something
    here? Is
    this possible at all?


    It works fine if I define the first struct memeber as char* x[ 3 ] ; but
    that
    is not what I'm after - I need arbitrary length char* arrays.


    As a last resort, I can do the initialization in the beginning of main
    (i.e. just myfoos[0].x = ptrToFirstStrArray ; // ...), but that's not very
    pretty...



    .::Antti::.



    Ps. I did consider "multistrings", e.g. "hello\0world\0", but that would
    be inconsistent w/ the presentation of array of string values used in
    other parts of the program.
     
    Antti Karanta, Mar 27, 2008
    #1
    1. Advertising

  2. Antti Karanta

    Eric Sosman Guest

    Antti Karanta wrote:
    >
    >
    > Hi!
    >
    > Is it possible to inline initialize a struct whose one member is a string
    > array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
    > something like this:
    >
    >
    > typedef struct {
    > char** x ;
    > int y ;
    > } Foo ;
    >
    > static const Foo myfoos[] = {
    > { { "hello", "world", NULL }, 12 },
    > { NULL, 0 }
    > } ;


    Define the arrays first, give them names, and use the
    names when you initialize the structs.

    static const char* fooArray0[] = {
    "hello", "world", NULL };
    static const char* fooArray1[] = {
    "there", "is", "no", "Cabal", NULL };
    static const Foo myfoos[] = {
    { fooArray0, 12 },
    { fooArray1, 42 },
    { NULL, 0 } };

    It's not as slick as if you could somehow make the arrays
    anonymous, but it works.

    --
     
    Eric Sosman, Mar 27, 2008
    #2
    1. Advertising

  3. Antti Karanta wrote:
    > ...
    > Is it possible to inline initialize a struct whose one member is a string
    > array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
    > something like this:
    >
    >
    > typedef struct {
    > char** x ;
    > int y ;
    > } Foo ;
    >
    > static const Foo myfoos[] = {
    > { { "hello", "world", NULL }, 12 },
    > { NULL, 0 }
    > } ;
    > ...


    You can do that in C99 by using compound literals

    static const Foo myfoos[] = {
    { (char*[]) { "hello", "world", NULL }, 12 },
    { NULL, 0 }
    };

    But in C89/90 you'll have to resort to multiple declarations, I believe.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 27, 2008
    #3
  4. On Thu, 27 Mar 2008 17:13:46 +0200, Andrey Tarasevich
    <andreytarasevich@xxx> wrote:

    > You can do that in C99 by using compound literals
    >
    > static const Foo myfoos[] = {
    > { (char*[]) { "hello", "world", NULL }, 12 },
    > { NULL, 0 }
    > };
    >
    > But in C89/90 you'll have to resort to multiple declarations, I believe.



    Thanks! Works great on mingw gcc. This would be exactly what I'm looking
    for,
    but microsoft cl compiler does not seem to support it (tried it on VS 2003
    & 2008).
    I'd rather not sacrifice code portability just for this small issue, so I
    guess
    I'll have to use the way suggested in the other reply, although it's not
    nearly as pretty... = /


    .::Antti::.
     
    Antti Karanta, Mar 29, 2008
    #4
  5. On Thu, 27 Mar 2008 16:23:21 +0200, Eric Sosman <Eric.Sosman@xxx> wrote:

    > Define the arrays first, give them names, and use the
    > names when you initialize the structs.
    >
    > static const char* fooArray0[] = {
    > "hello", "world", NULL };
    > static const char* fooArray1[] = {
    > "there", "is", "no", "Cabal", NULL };
    > static const Foo myfoos[] = {
    > { fooArray0, 12 },
    > { fooArray1, 42 },
    > { NULL, 0 } };
    >
    > It's not as slick as if you could somehow make the arrays
    > anonymous, but it works.



    Hmm, mingw gcc gives a warning for these lines:

    { fooArray0, 12 },
    { fooArray1, 42 },

    "initialization from incompatible pointer type"

    MS Visual C++ says: "warning C4090: 'initializing' : different 'const'
    qualifiers"

    Removing the const qualifier from the definitions of fooArray0 and
    fooArray1 or
    adding it to the struct member (const char** x) seems to resolve the
    problem.

    Thanks for this. You're right - it's not as slick as I'd like, but at
    least it
    works portably.



    .::Antti::.
     
    Antti Karanta, Mar 29, 2008
    #5
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,431
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    2
    Views:
    758
    David Harmon
    Sep 20, 2006
  3. toton
    Replies:
    5
    Views:
    947
    Victor Bazarov
    Sep 28, 2006
  4. aaragon
    Replies:
    2
    Views:
    633
    James Kanze
    Nov 2, 2008
  5. Tuan  Bui
    Replies:
    14
    Views:
    507
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page