469,898 Members | 1,707 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

"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 1976
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marek Lewczyk | last post: by
2 posts views Thread by thomasfj | last post: by
reply views Thread by Codex Twin | last post: by
3 posts views Thread by usr.root | last post: by
4 posts views Thread by Yair | last post: by
7 posts views Thread by php_mysql_beginer911 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.