473,756 Members | 6,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Colon (:) syntax in defining fields in a struct

Hello all,

I recently came across the following segment of code that defines a C
struct:

typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;

What do the numbers 4 and 2 refer to?

If I define a second struct as below:

typedef struct
{
unsigned char unused_bits;
unsigned char wchair_state;
} abc;
and then declare

void main(void)
{
xyz _xyz;
abc _abc;
}

In terms of memory allocation, is there any difference between that
allocated for _xyz and _abc?

Any feedback would be much appreciated.

Thanks
Raj
Nov 14 '05 #1
16 12054
":" means bit-allocation.
System will allocate 1 byte to struct _xyz and 2 byte to struct _abc.
Here is mem structure chart:
_______________ _______________ _______________ ____
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.u_bits ->|<- _xyz.w->|

_______________ __
|1|2|3|4|5|6|7| 8| <---- _abc.u_bits;
+-+-+-+-+-+-+-+-+
|1|2|3|4|5|6|7| 8| <---- _abc.w
-----------------
That is,and works .
I really want you get it.

--
Shark Vanue
Nov 14 '05 #2
ra********@hotm ail.com (Raj Kotaru) wrote:
# Hello all,
#
# I recently came across the following segment of code that defines a C
# struct:
#
# typedef struct
# {
# unsigned char unused_bits:4;
# unsigned char wchair_state:2;
# } xyz;
#
# What do the numbers 4 and 2 refer to?

unused_bits is four bits wide and wchair_state is two bits. The fields may
be packed as tightly as possible, in one char sized unit possibly.

On a typical CPU and C implementation, without the field widths, the
struct would be two characters wide, and only one character wide with
the above.

#
# If I define a second struct as below:
#
# typedef struct
# {
# unsigned char unused_bits;
# unsigned char wchair_state;
# } abc;
#
#
# and then declare
#
# void main(void)
# {
# xyz _xyz;
# abc _abc;
# }
#
# In terms of memory allocation, is there any difference between that
# allocated for _xyz and _abc?

Add
printf("%d %d\n",sizeof(xy z),sizeof(abc)) ;
I would expect to see it print
1 2

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Nov 14 '05 #3
ra********@hotm ail.com (Raj Kotaru) writes:
I recently came across the following segment of code that defines a C
struct:

typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;


Look up "bit fields" in any C textbook.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Shark Venue wrote:

":" means bit-allocation.
System will allocate 1 byte to struct _xyz and 2 byte to struct _abc.
Here is mem structure chart:
_______________ _______________ _______________ ____
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.u_bits ->|<- _xyz.w->|


I don't believe the standard specifies the order in which bits are
allocated. If that's the case, you may have:

-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.w->|<- _xyz.u_bits ->|

Or even:

-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.u_bits ->|
-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.w->|

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #5
Groovy hepcat SM Ryan was jivin' on Sat, 28 Aug 2004 16:33:30 -0000 in
comp.lang.c.
Re: Colon (:) syntax in defining fields in a struct's a cool scene!
Dig it!
ra********@hot mail.com (Raj Kotaru) wrote:
# Hello all,
#
# I recently came across the following segment of code that defines a C
# struct:
#
# typedef struct
# {
# unsigned char unused_bits:4;
# unsigned char wchair_state:2;
# } xyz;
#
# What do the numbers 4 and 2 refer to?

unused_bits is four bits wide and wchair_state is two bits. The fields may
be packed as tightly as possible, in one char sized unit possibly.

On a typical CPU and C implementation, without the field widths, the
struct would be two characters wide, and only one character wide with
the above.
It should be pointed out, though, that unsigned char is non-portable
for the type of a bit field. Only a qualified or unqualified version
of signed int, unsigned int or _Bool (in C99) are portable.
# If I define a second struct as below:
#
# typedef struct
# {
# unsigned char unused_bits;
# unsigned char wchair_state;
# } abc;
#
# and then declare
#
# void main(void)
Pay attention (Raj Kotaru)! I'm only going to say this a billion
times or so. The main() function is supposed to return an int, not
void. Portable return values for main() are 0, EXIT_SUCCESS and
EXIT_FAILURE, the latter two being macros defined in stdlib.h.

int main(void)# {
# xyz _xyz;
# abc _abc;
return 0;# }
#
# In terms of memory allocation, is there any difference between that
# allocated for _xyz and _abc?

Add
printf("%d %d\n",sizeof(xy z),sizeof(abc)) ;
I would expect to see it print
1 2


I would expect the unexpected. When you invoke the wrath of the
undefined behaviour gods, you never really know what to expect. Well,
sometimes you can make a reasonable guess, but it can always go wrong
and do something you least expect.
Instead, try this:

printf("sizeof xyz = %lu, sizeof abc = %lu\n",
(unsigned long)sizeof xyz,
(unsigned long)sizeof abc);

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #6
Raj Kotaru wrote:
Hello all,

I recently came across the following segment of code that defines a C
struct:

typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;

What do the numbers 4 and 2 refer to?
Look up for "Bit Fields and C " in Google.

If I define a second struct as below:

typedef struct
{
unsigned char unused_bits;
unsigned char wchair_state;
} abc;
and then declare

void main(void)
{
xyz _xyz;
abc _abc;
}

In terms of memory allocation, is there any difference between that
allocated for _xyz and _abc?

Bit Fields are generally used to pack data to conserve spacee.
So the two structs would have different sizes altogether.
--
Karthik.
Nov 14 '05 #7
Peter Shaggy Haywood wrote:
# In terms of memory allocation, is there any difference between that
# allocated for _xyz and _abc?

Add
printf("%d %d\n",sizeof(xy z),sizeof(abc)) ;
I would expect to see it print
1 2

I would expect the unexpected. When you invoke the wrath of the
undefined behaviour gods, you never really know what to expect. Well,
sometimes you can make a reasonable guess, but it can always go wrong
and do something you least expect.
Instead, try this:

printf("sizeof xyz = %lu, sizeof abc = %lu\n",
(unsigned long)sizeof xyz,
(unsigned long)sizeof abc);


Ok, you've got me! Why is the above modification necessary and why
could the original invoke undefined behavior?

P.S.
I waited about two hours for my reply to show up on my news server and
still haven't seen it. I apologize if the original finally shows up.

--
Sean
Nov 14 '05 #8
In <1J************ ***@news.abs.ne t> "Fao, Sean" <en**********@y ahoo.comI-WANT-NO-SPAM> writes:
Peter Shaggy Haywood wrote:
>># In terms of memory allocation, is there any difference between that
# allocated for _xyz and _abc?

Add
printf("%d %d\n",sizeof(xy z),sizeof(abc)) ;
I would expect to see it print
1 2

I would expect the unexpected. When you invoke the wrath of the
undefined behaviour gods, you never really know what to expect. Well,
sometimes you can make a reasonable guess, but it can always go wrong
and do something you least expect.
Instead, try this:

printf("sizeof xyz = %lu, sizeof abc = %lu\n",
(unsigned long)sizeof xyz,
(unsigned long)sizeof abc);


Ok, you've got me! Why is the above modification necessary and why
could the original invoke undefined behavior?


What is the type expected by %d in a printf format?
What is the type of the value yielded by the sizeof operator?
What happens when the two types don't match?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
Fao, Sean wrote:
Peter Shaggy Haywood wrote:
>># In terms of memory allocation, is there any difference between that # allocated for _xyz and _abc?

Add
printf("%d %d\n",sizeof(xy z),sizeof(abc)) ;
I would expect to see it print
1 2


I would expect the unexpected. When you invoke the wrath of the
undefined behaviour gods, you never really know what to expect. Well,
sometimes you can make a reasonable guess, but it can always go wrong
and do something you least expect.
Instead, try this:

printf("sizeof xyz = %lu, sizeof abc = %lu\n",
(unsigned long)sizeof xyz,
(unsigned long)sizeof abc);

Ok, you've got me! Why is the above modification necessary and why
could the original invoke undefined behavior?


Actually, let me see if I can figure this out for myself.

The sizeof operator results in something of type size_t, which I assume
on some implementations *could* be an unsigned long. If that's correct,
why not unsigned long long?

But this doesn't answer my question as to where the UD could come into
play. Obviously, the original code with a %d could not display any
number over INT_MAX; however, the result would still be defined (at
least I _think_ rolling over is defined)

Am I missing something?

--
Sean
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1589
by: wesley | last post by:
Hi, What's the reasoning of having struct in .Net? Since they hardly differ with classes? Are there any advantages using struct instead of classes? Thanks
3
4612
by: Bob Hairgrove | last post by:
Surely this is a no-brainer, but sometimes I think I must have no brain (no comments, please<g>). Anyway, I have the following struct containing bit fields: struct ExtPenStyle { unsigned long style : 4; unsigned long : 4; // unused unsigned long endcap : 2; unsigned long : 2; // unused
2
2952
by: Simon Elliott | last post by:
I have some legacy code which initialises an array using C syntax like this: struct bar { int i1_; int i2_; bool b1_; };
5
3828
by: Sönke Tesch | last post by:
Hi everybody, I have a problem with the following piece of code: 141: /* A data block to manage a single log target: */ 142: typedef struct { 143: apr_reslist_t *dbs; /* connection pool */ 144: 145: const char *uri; /* the complete log uri.. */
2
5056
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
4
2373
by: Leo | last post by:
Hello, I have a C dll with a method signature of: int activate(datastruct *data) where datastruct is defined as: typedef struct datastruct { long result;
1
10515
by: Svenn Are Bjerkem | last post by:
Hi Forum, I am not very much experienced with c++, but I have unfortunately programmed a lot of c and have a problem understanding the purpose of defining a struct in the private part of a class. I am looking at the example code for Interviews in the Qt-4 example code, which unfortunately hasn't been documented yet. http://doc.trolltech.com/4.2/demos-interview.html
10
2577
by: Mosfet | last post by:
Hi, I would like more info about deriving from an existing C struct. Let's say I am fed up with always writing the same code shown below : MYSTRUCT foo; memset( &foo, 0, sizeof(MYSTRUCT) ); foo.cbSize = sizeof(MYSTRUCT);
4
1277
by: stupot1987 | last post by:
hey im new to this site but found it has helped me in the past with other similar problems. i want to be able to compare 2 fields in my sql my code is as follows: Private Sub Command1_Click() Exit Sub Adodc1.RecordSource = "SELECT Stock., Stock.Description, Stock., Stock. FROM Stock; WHERE Stock.) < Stock.;" Adodc1.Recordset.Requery Adodc1.Refresh DataGrid1.Refresh
0
9117
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9676
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
9541
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...
0
8542
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6390
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
4955
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...
0
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3651
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
2
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.