comp.lang.c
[Top] [All Lists]

Re: Inline initialization of a struct containing a string array

Subject: Re: Inline initialization of a struct containing a string array
From: Eric Sosman
Date: Thu, 27 Mar 2008 10:23:21 -0400
Newsgroups: comp.lang.c


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@xxxxxxx

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy