473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

anonymous struct

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
5 14509

"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
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: JKop | last post by:
You know how from time to time, you want to have an array which you can access via: array_name; But also, you'd like the more user-friendly option: array_name.element1 array_name.element2
1
by: mark fine | last post by:
////////////// snippet //////////////// struct t { static struct { } a; }; struct { } t::a; int main(void) {
11
by: G Fernandes | last post by:
I've heard some mention "anonymous" objects in c.l.c and other places (conversations), and I was wondering what exactly these are. Anonymous seems to imply that there is no name, but no name...
1
by: qwerty2_reverse_iterator | last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.) I get errors when I don't initialize all the const pointer fields of an anonymous union in a struct. Example: //T2.h...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
7
by: David Resnick | last post by:
I'm faced with a header with anonymous structures declared inside a union like this: union msg { struct { int a; int b; } s1; struct {
16
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.