"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6-********************@comcast.com...
Jim Langston wrote: "Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message
news:dk**********@news.onet.pl... here's what i have:
struct SCSI_CDB {
...
union {
struct {
unsigned char address0;
unsigned char address1;
unsigned char address2;
};
unsigned char address[3];
};
...
};
then i do:
SCSI_CDB cdb;
cdb.address0 = 0xFF;
MSVC++7 compiles it. GCC with -ansi flag says it's forbiden to have
struct without a name.
should i do this:
struct {
unsigned char address0;
...
} adr;
and then:
cdb.adr.address0 = 0xFF;
is it legal according to the standard to ommit the adr?
I would rather question is it logical to have a structure without a
name. Why have a structure without a name? For what purpose? If
it's just to group the elements together you could do that with a
comment.
And I doubt if it's legal according to the standard.
An anonymous struct can exist, but since you can't refer to it by a name,
you need to instantiate it right away (unlike an anonymous union). So,
a struct definition without an object name following it would not be
useful at all. The solution the OP has already stumbled upon was to have
a member name 'adr' right after the struct definition. AFAICT, that is
the only way to make use of an anonymous struct (whether inside a union
or in any other place).
Victor
victor,
i understand your explanation. thanks.
i posted the question, because i was wondering if such construct is possible
when it comes to unions. basically group some primitives as one part of the
union, and then make the second part an array of bytes. msvc++ and gcc both
took it (before disabling extensions or ansi compatibility, of course:)