C/C++ syntax

  • Thread starter Thread starter Al_C
  • Start date Start date
A

Al_C

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?
 
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;
 
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
 
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)

MY_variable.combi = 10

Yes. combi is used like that.
Your structure contains a union that can be used through the name 'shared'
as either the optical member or the battery_level_member.

To give you even more detail, the size of your struct depends on the pack
level that is used. structure members are aligned on memory offsets that are
either multiples of the pack level or the element size, whichever is smaller.

this means that the size of the following structure is 8 bytes instetad of 5
because b starts at the first 4 byte offset after a.
struct A
{
char a;
long b
};

for more information look up the pack pragma and the sizeof keyword. they
contain a detailed explanation of how and why this is done.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
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
 
Back
Top