472,805 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 2143
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.