470,849 Members | 1,098 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

C/C++ syntax

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?
Mar 1 '06 #1
4 1150
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?

Mar 1 '06 #2
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?


Mar 1 '06 #3
> 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


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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 1 '06 #4
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?



Mar 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
22 posts views Thread by Tuang | last post: by
14 posts views Thread by Sandy Norton | last post: by
16 posts views Thread by George Sakkis | last post: by
23 posts views Thread by Carter Smith | last post: by
19 posts views Thread by Nicolas Fleury | last post: by
4 posts views Thread by Jeremy Yallop | last post: by
177 posts views Thread by C# Learner | last post: by
4 posts views Thread by Bob hotmail.com> | last post: by
3 posts views Thread by Manuel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.