473,406 Members | 2,356 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.

"union" syntax

Hi all,

I'm struggling to do something really basic. How do I declare a structure
that has a union that maps an array of 3 chars with 3 distinct char
variables. In other words, I want the following two statements to put the
same value into the same byte in the structure:

myStruct.Vars[1] = 5;
myStruct.Var1 = 5;

TIA - Bob
Sep 19 '07 #1
6 2180
How about just including this somewhere:

#define myStruct.Var1 myStruct.Var[1]

To be honest, not sure if the above works, and it is a purely syntactical
solution, and may not address the reasons WHY you want this capability.

[==Peter==]

"Bob Altman" <rd*@nospam.nospamwrote in message
news:Oa****************@TK2MSFTNGP04.phx.gbl...
Hi all,

I'm struggling to do something really basic. How do I declare a structure
that has a union that maps an array of 3 chars with 3 distinct char
variables. In other words, I want the following two statements to put the
same value into the same byte in the structure:

myStruct.Vars[1] = 5;
myStruct.Var1 = 5;

TIA - Bob

Sep 19 '07 #2
Well, that's certainly thinking outside of the box. But I'd be surprised if
it compiles.

As for why I want this: I have a structure that is used to pass data into
my public API:

struct MyStates {
BOOL X_Axis_Enabled;
BOOL Y_Axis_Enabled;
BOOL Z_Axis_Enabled;
}

Now, I've decided that it would be very convenient for me to be able to
access the members of this structure as though they were members of an
array, like this:

struct MyStates {
BOOL Enabled[3];
}

But I can't change the structure in a way that will break existing code that
accesses the individual member variables.

- Bob

"Peter Oliphant" <po*******@roundtripllc.comwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
How about just including this somewhere:

#define myStruct.Var1 myStruct.Var[1]

To be honest, not sure if the above works, and it is a purely syntactical
solution, and may not address the reasons WHY you want this capability.

[==Peter==]

Sep 19 '07 #3
On Wed, 19 Sep 2007 11:52:08 -0700, "Bob Altman" <rd*@nospam.nospamwrote:
>Hi all,

I'm struggling to do something really basic. How do I declare a structure
that has a union that maps an array of 3 chars with 3 distinct char
variables. In other words, I want the following two statements to put the
same value into the same byte in the structure:

myStruct.Vars[1] = 5;
myStruct.Var1 = 5;
You could do it with:

union U
{
struct
{
char Var0;
char Var1;
char Var2;
};
char Vars[3];
};

This makes use of an anonymous struct, which is a VC extension, so it's not
going to be portable.

--
Doug Harrison
Visual C++ MVP
Sep 19 '07 #4
On Wed, 19 Sep 2007 11:58:39 -0700, "Peter Oliphant"
<po*******@roundtripllc.comwrote:
>How about just including this somewhere:

#define myStruct.Var1 myStruct.Var[1]

To be honest, not sure if the above works
It does not. You can't define a macro name that contains a dot.

--
Doug Harrison
Visual C++ MVP
Sep 19 '07 #5
Thanks Doug, that's just what I was looking for.

- Bob

"Doug Harrison [MVP]" <ds*@mvps.orgwrote in message
news:du********************************@4ax.com...
On Wed, 19 Sep 2007 11:52:08 -0700, "Bob Altman" <rd*@nospam.nospam>
wrote:
>>Hi all,

I'm struggling to do something really basic. How do I declare a structure
that has a union that maps an array of 3 chars with 3 distinct char
variables. In other words, I want the following two statements to put the
same value into the same byte in the structure:

myStruct.Vars[1] = 5;
myStruct.Var1 = 5;

You could do it with:

union U
{
struct
{
char Var0;
char Var1;
char Var2;
};
char Vars[3];
};

This makes use of an anonymous struct, which is a VC extension, so it's
not
going to be portable.

--
Doug Harrison
Visual C++ MVP

Sep 19 '07 #6
Bob Altman wrote:
Well, that's certainly thinking outside of the box. But I'd be surprised if
it compiles.

As for why I want this: I have a structure that is used to pass data into
my public API:

struct MyStates {
BOOL X_Axis_Enabled;
BOOL Y_Axis_Enabled;
BOOL Z_Axis_Enabled;
}

Now, I've decided that it would be very convenient for me to be able to
access the members of this structure as though they were members of an
array, like this:

struct MyStates {
BOOL Enabled[3];
}

But I can't change the structure in a way that will break existing code that
accesses the individual member variables.
Bob:

How about

struct MyStates
{
BOOL X_Axis_Enabled;
BOOL Y_Axis_Enabled;
BOOL Z_Axis_Enabled;
BOOL& operator[](int i)
{
BOOL* enabled[3];
enabled[0] = &X_Axis_Enabled;
enabled[1] = &Y_Axis_Enabled;
enabled[2] = &Z_Axis_Enabled;
return *(enabled[i]);
}
};

int main()
{
MyStates myStates;
myStates[0] = TRUE;
myStates[1] = FALSE;
myStates[2] = TRUE;
return 0;
}

--
David Wilkinson
Visual C++ MVP
Sep 20 '07 #7

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

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
0
by: Marek Lewczyk | last post by:
Hello, Currently I'm testing my app using MySQL 4.1.0 version, and I have a strange error during execution a union query. (SELECT IF(_DAT.pri <=> null, null, ROUND(_DAT.pri/1.22)) AS pri_net,...
2
by: thomasfj | last post by:
Hi, Accroding to MS documentation the use of union and list elements in XSD simpleType (valid according to W3C standards) is valid used as an dataset schema...but it's not!! When loading the...
0
by: Codex Twin | last post by:
hello group: The following is a fragment from a schema which defines the EWethnicCategoryStructure type. As you can see, its type is defined by the SimpleType enumeration EWethnicCategoryType....
1
by: CrystalDBA | last post by:
I usually design applications in SQL Server and Crystal Reports. I now need to create a crystal report on an MS Access database. I have two tables: Services: Date datetime Entry text...
3
by: usr.root | last post by:
hi,who knows how does the compiler work after it see the "union" word in your code .
4
by: Yair | last post by:
Hi, I need to shift the bits of a float type. In order to do so, I declared the following struct: private struct unionIntFloatType { public float m_asFloat;
3
by: John Sasso | last post by:
In my Yacc .y file I defined: %union { int value; struct Symbol Sym; } The Symbol struct I defined in a header file I #included in the Prologue section of the .y file as:
7
by: php_mysql_beginer911 | last post by:
Hi .. hope someone will help i am trying to figure it out why i cannot post string "union select" every time i try to post data which content union and select .. the page doesn't get posted and...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.