473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.address 0 = 0xFF;

is it legal according to the standard to ommit the adr?

Oct 31 '05 #1
5 14525

"Martin Vorbrodt" <mv*******@pocz ta.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.address 0 = 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*******@pocz ta.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.address 0 = 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.********@com Acast.net> wrote in message
news:6-*************** *****@comcast.c om...
Jim Langston wrote:
"Martin Vorbrodt" <mv*******@pocz ta.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.address 0 = 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.********@com Acast.net> wrote in message
news:6-*************** *****@comcast.c om...
Jim Langston wrote:
"Martin Vorbrodt" <mv*******@pocz ta.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.address 0 = 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
2730
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
5495
by: mark fine | last post by:
////////////// snippet //////////////// struct t { static struct { } a; }; struct { } t::a; int main(void) {
11
3299
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 implies that one can't access the object, which implies that the object would be useless. So what exactly are they?
1
2405
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 typedef int Type1; typedef float Type2; struct S1 {
6
6989
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 classes. I was thinking of a class Agenda. For it I would use a class Person which also uses another class Date for her birthday. When I was modeling Person, I made an atribute to be an object of class Date. Suddenly I thought that maybe it was a...
7
8873
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
6459
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 enum but its members can be addressed with '.'? I don't want to have to do this using #defines, as it would be far too messy. code: static struct { static const int baz = 1; } bar;
3
7726
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
3939
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 defined entities to the source file in which the anonymous namespace is defined. Entities defined in the anonymous namespace are comparable to C’s static functions and variables. In C++ the static keyword can still be used, but its use is more
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.