By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

anonymous struct

P: n/a
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?

Oct 31 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"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.
Nov 1 '05 #2

P: n/a
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
Nov 1 '05 #3

P: n/a

"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


Well, I knew about anonymous structures, I use them all the time in my
classes. But of course it's instantized by a name. Which is what my
quesiton was about.
Nov 1 '05 #4

P: n/a
"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:)
Nov 1 '05 #5

P: n/a
Martin Vorbrodt wrote:
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:)


OK, I looked it up. 9.5 describes unions and anonymous unions in
particular. It says that anonymous unions can only contain data
members, an only non-static ones. Nested types are not allowed.
I suppose you _can_ (should be able to) define a non-static member
whose type is an anonymous struct, but that would require the name
of the object to follow the struct definition. That's what you've
already found.

V
Nov 1 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.