Inline initialization of a struct containing a string array

A

Antti Karanta

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

Eric Sosman

Antti said:
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.
 
A

Andrey Tarasevich

Antti said:
...
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.
 
A

Antti Karanta

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::.
 
A

Antti Karanta

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top