473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1266
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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;

"Al_C" <ac****@bicnet. net> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;
MY_variable.com bi = 10

regards,
Al

"Kevin Frey" <ke**********@h otmail.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;

"Al_C" <ac****@bicnet. net> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;


MY_variable.com bi = 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_m ember.

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************* *********@hotma il.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.optic al = 1;
My_Struct.batte ry_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******** ******@TK2MSFTN GP09.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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;


MY_variable.com bi = 10

regards,
Al

"Kevin Frey" <ke**********@h otmail.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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.sha red.optical = 1;
My_Variable.sha red.battery_lev el = 0;

"Al_C" <ac****@bicnet. net> wrote in message
news:%2******** ********@TK2MSF TNGP10.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
34270
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
22
3433
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if the need arises. If I have to go off and look it up, as I increasingly have to do with Perl's ever hairier syntax, I'm more likely to just skip it, making me even less likely to remember the syntax the next time. So I hear that Python is easier...
14
2318
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def __init__(self, name):
16
2611
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very useful for list/generator comprehensions, for example being able to write something like: instead of the less elegant explicit loop version that has to check for the length of each sequence. What do you think ? George
23
2540
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line by their authors. There are lots of them out there but this selection cuts out the junk. If you know of any other good books that are freely available please post a link to them here and I'll consider adding them to the site.
19
2981
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
4
3785
by: Jeremy Yallop | last post by:
Looking over some code I came across a line like this if isalnum((unsigned char)c) { which was accepted by the compiler without complaint. Should the compiler have issued a diagnostic in this case? (I think it's not required to, but I'd like confirmation). Jeremy.
177
7103
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
4
7628
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax highlighting in C# but I have yet to find anything useful I know there are people at MS that know this stuff like the front of their hand and I know there are many people out on the web that are proficient in doing this as well but it seems nobody...
3
16253
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very strange error. In your experience, where I should looking to find the real error? Surely the sintax of glut is correct... gcc.exe -c glut_bitmap.c -o glut_bitmap.o -I"C:/Dev-Cpp/include" -I"../../include" -D__GNUWIN32__ -W -DWIN32 -DNDEBUG...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.