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? 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.
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 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.
"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:)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
by: mark fine |
last post by:
////////////// snippet ////////////////
struct t {
static struct { } a;
};
struct { } t::a;
int main(void)
{
|
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?
|
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
{
|
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...
| |
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 {
|
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;
|
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;
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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
| |