GCC structure initialization

S

Svein E. Seldal

Hello,

I have this:

struct mystruct {
int a;
union {
int b;
int c;
};
};

When I initialize it:

struct mystruct implement = {
.a = 1,
.b = 2
};

I get the following error from gcc:

error: unknown field `b' specified in initializer

Is it possible to do this kind of initialization alltogether with gcc?
How can I initialize the anonymous union in the struct listed above?

I know this is a gcc feature, but I wondered if there anyone here that
might know this, please.


Thanks,
Svein
 
A

Alex

[snip]
How can I initialize the anonymous union in the struct listed above?

By making it not anonymous? Questions regarding GCC-specific features should
be asked in a GCC group. Although IIRC, C99 has something similar (or
identical).

Alex
 
M

Martin Ambuhl

Svein said:
Hello,

I have this:

struct mystruct {
int a;
union {
int b;
int c;
};
};

When I initialize it:

struct mystruct implement = {
.a = 1,
.b = 2
};

I get the following error from gcc:

error: unknown field `b' specified in initializer

Is it possible to do this kind of initialization alltogether with gcc?
How can I initialize the anonymous union in the struct listed above?

I know this is a gcc feature, but I wondered if there anyone here that
might know this, please.

Anonymous unions are off-topic here. A gnu* newsgroup would do better.
Obviously, naming the union would work here.

<ot>
In your case either
struct mystruct implement = {
.a = 1, {2}
};
or
struct mystruct implement = {
.a = 1
};
implement.b = 2;
should work.
</ot>
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top