473,406 Members | 2,849 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,406 software developers and data experts.

Is this legal code?

I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
Code Composer Studio for an embedded application.

This code compiles under Visual Studio, and lets the program reference x, y,
and z. Under a Code Composer Studio compiler, the compiler warns: "this
declaration doesn't declare anything", then gives errors for all subsequent
references to x, y, and z.

union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

My C++ book implies that the struct declaration shouldn't declare any data
because there is no assigned name, and I believe that is why Code Composer
is complaining.

Is this some legal C++ extension that is just not supported by Code
Composer, or is it a non-standard Visual Studio extension?

Ed.
Jul 22 '05 #1
3 1959

"Ed J" <je*@privacy.net> wrote in message
news:S4********************@adelphia.com...
I'm trying to port some C++ code from MS Visual Studio 6.0 to a version of
Code Composer Studio for an embedded application.

This code compiles under Visual Studio, and lets the program reference x, y, and z. Under a Code Composer Studio compiler, the compiler warns: "this
declaration doesn't declare anything", then gives errors for all subsequent references to x, y, and z.

union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

My C++ book implies that the struct declaration shouldn't declare any data
because there is no assigned name, and I believe that is why Code Composer is complaining.

Is this some legal C++ extension that is just not supported by Code
Composer, or is it a non-standard Visual Studio extension?

Ed.


It's usually called an anonymous struct, it's not standard C++. Anonymous
unions on the other hand are standard.

john
Jul 22 '05 #2
Ed J wrote:
union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};


A few points:
1. Nested types aren't allowed to be declared in anonymous unions (but
you could work around that with a non-nested type)
2. At global/namepsace scope the union must be declared static
3. Although v and x,y,z occupy the same memory space, there is no
guarnatee the the three values of v are the same values as x,y,z
(alignment requirements could align the variables differently)
--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>

"What suffering would man know if not for his own?"
Jul 22 '05 #3
Ed J posted:
union{
struct{
float x, y, z; // Named element representation
};
float v[3]; // Vector component
};

This would be exactly like writing:
int;
float;
You're not naming the variable, nor are you defining a union. The following
would be valid:
union Cat {
struct {
float x, y, z;
};

float v[3];
};

Here I have defined a union... (I hasten to use the term "template"!)...
from which I can in the future *declare* variables, as in:

Cat cat;

OR, going down another road:

union {
struct {
float x, y ,z;
};

float v[3];
} cat;
Your original code would only be valid in the following circumstance:

struct Choc {
union {
struct {
float x, y, z;
};

float v[3];
};
};
-JKop

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
0
by: mykidisgollum | last post by:
Greetings, I have code which prints a document who's attributes are saved as flags in a database. One of the those attributes is letter or legal. When I am printing, I use the following...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
17
by: wana | last post by:
I was reading through original source code of ping for some insight and was confused by unusual code. Entire listing available at: http://www.ping127001.com/pingpage/ping.html #include...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
7
by: Don Taylor | last post by:
Found in a style guide (http://www.artlogic.com/careers/styleguide.html) ----------------------------------------------------------------------- Another case where "unnecessary" braces should be...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
1
by: unkstar | last post by:
I've written a extentable_buffer class, which allows me to acquire bigger buffer whenever I need. The original version has a conversion operator to void*, it's enought for that good old time....
19
by: Charles Sullivan | last post by:
A C program with code typified by the following pared-down example has been running after compilation on numerous compilers for several years. However with a fairly recent GCC compiler it results...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.