468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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:

"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.

Mar 27 '08 #1
2 8067
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.

--
Er*********@sun.com
Mar 27 '08 #2
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
Mar 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rudy Velthuis | last post: by
7 posts views Thread by Hendrik Schober | last post: by
26 posts views Thread by alberto | last post: by
4 posts views Thread by Michael Brennan | last post: by
12 posts views Thread by JackYee123 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.