Inline initialization of a struct containing a string array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antti Karanta

    Inline initialization of a struct containing a string array



    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:

    "initializa tion 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 = ptrToFirstStrAr ray ; // ...), but that's not very
    pretty...



    .::Antti::.



    Ps. I did consider "multistrin gs", 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.

  • Eric Sosman

    #2
    Re: Inline initialization of a struct containing a string array

    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@sun .com

    Comment

    • Andrey Tarasevich

      #3
      Re: Inline initialization of a struct containing a string array

      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

      Comment

      Working...