The combi member variable is just another variable in the struct.
Unions are usually not used exclusively by themselves because they
intrinsically (ie. by themselves) contain no way of telling you which
particular variable they are currently holding.
So the union often be used inside a struct, and a separate member of the
struct will be an "indicator" as to the type of information to expect in the
union. The classic example of this (as far as I'm concerned) is the Windows
VARIANT structure. In simple terms in contains a struct member vt to
indicate what type of data it is holding, and a large union holding all the
potential different data types.
So, having said that, one would expect the combi member variable would be
giving some indication of the type of data, and hence an indication of what
value within the union is being used, although your example doesn't provide
sufficient detail to prove that is the case.
Oh, and something I failed to mention - the size of a union is the size of
its largest element.
C++ also supports the concept of "anonymous unions", these eliminate the
need to declare a variable name, so the inner members of the union become
part of the same scope in which the union itself is declared. To use your
example:
typedef struct
{
union
{
OPTICAL_REPORT optical;
UINT8 battery_level;
};
UINT8 combi;
} AL_Struct;
AL_Struct My_Struct;
My_Struct.optical = 1;
My_Struct.battery_level = 10;
My_Struct.combo = 5;
Notice that there is no "shared" member variable being referenced anymore.
The downside is that it is [even more] impossible to tell, looking at the
code, that optical/battery_level are actually occupying the same memory
space of the struct, hence over-writing each other. At least "shared" gives
some hint to the read of the code that it might be a union.
Kevin
"Al_C" <ac****@bicnet.net> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
Thanks Kevin,
Just so I understand it solid I can follow what you said, but where does
the combi come in?
Is that an add on to the basic struct? (AL_struct) AL_Struct My_Variable;
My_Variable.shared.optical = 1;
My_Variable.shared.battery_level = 0;
MY_variable.combi = 10
regards,
Al
"Kevin Frey" <ke**********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl... The "shared" is the name of the union. The syntax is the short-hand way
of declaring the type and the variable name at the same time.
As in:
AL_Struct My_Variable;
My_Variable.shared.optical = 1;
My_Variable.shared.battery_level = 0;
"Al_C" <ac****@bicnet.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... OK, my K&R is about 10 years old now, I'm tring to understand the
following:
typedef struct
{
union
{
OPTICAL_REPORT optical;
UINT8 battery_level;
} shared;
UINT8 combi;
} AL_Struct;
I;m ok with the union piece, it's either one or the other in the same
memory space.
But I don't understand the shared term.
If I did a sizeof, would it be 16 bits?
Thanks,
Al
From what I gather the OPTICAL_REPORT data type is a 16 bit ints.
So is the shared term saying I have either the battery_level or the
combi available to me?