Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Inline initialization of a struct containing a string array

Reply
Thread Tools

Inline initialization of a struct containing a string array

 
 
Antti Karanta
Guest
Posts: n/a
 
      03-27-2008


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.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-27-2008
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-27-2008
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
 
Reply With Quote
 
Antti Karanta
Guest
Posts: n/a
 
      03-29-2008

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

 
Reply With Quote
 
Antti Karanta
Guest
Posts: n/a
 
      03-29-2008

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments