Re: Colon (:) syntax in defining fields in a struct
From: Peter (phaywood_at_alphalink.com.au.NO.SPAM)
Date: 09/01/04
- Next message: Douglas A. Gwyn: "Re: addresses and integers"
- Previous message: Allin Cottrell: "Re: C 99 compiler access"
- Next in thread: Karthik Kumar: "Re: Colon (:) syntax in defining fields in a struct"
- Maybe reply: Karthik Kumar: "Re: Colon (:) syntax in defining fields in a struct"
- Reply: Fao, Sean: "Re: Colon (:) syntax in defining fields in a struct"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 01 Sep 2004 05:09:45 GMT
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!
>raj_kotaru@hotmail.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(xyz),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 "technically correct" English; but since when was rock & roll "technically correct"?
- Next message: Douglas A. Gwyn: "Re: addresses and integers"
- Previous message: Allin Cottrell: "Re: C 99 compiler access"
- Next in thread: Karthik Kumar: "Re: Colon (:) syntax in defining fields in a struct"
- Maybe reply: Karthik Kumar: "Re: Colon (:) syntax in defining fields in a struct"
- Reply: Fao, Sean: "Re: Colon (:) syntax in defining fields in a struct"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|