"union" syntax

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

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
 
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==]
 
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
 
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.
 
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.
 
Thanks Doug, that's just what I was looking for.

- Bob

Doug Harrison said:
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.
 
Bob said:
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);
}
};

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