By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

Volatile bitfields

alexis4
100+
P: 113
Hi!

What is the difference between the below 3 cases in ANSI C?

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. volatile unsigned char a : 1,
  4.                        b : 1,
  5.                        c : 1,
  6.                        d : 1,
  7.                        e : 1,
  8.                        f : 1,
  9.                        g : 1,
  10.                        h : 1;
  11. } MyStruct1;
  12. MyStruct1    struct1;
  13.  
  14.  
  15. typedef struct
  16. {
  17. unsigned char a : 1,
  18.               b : 1,
  19.               c : 1,
  20.               d : 1,
  21.               e : 1,
  22.               f : 1,
  23.               g : 1,
  24.               h : 1;
  25. } MyStruct2;
  26. volatile MyStruct2    struct2;
  27.  
  28.  
  29. typedef struct
  30. {
  31. volatile unsigned char a : 1,
  32.                        b : 1,
  33.                        c : 1,
  34.                        d : 1,
  35.                        e : 1,
  36.                        f : 1,
  37.                        g : 1,
  38.                        h : 1;
  39. } MyStruct3;
  40. volatile MyStruct3    struct3;

I know what a volatile and what a bitfield is, so it is not necessary to explain those.

Furthermore, I think that struct1, struct2 and struct3 are exactly the same, but I'm not sure and that's why I'm asking!


Thanks in advance!
Sep 20 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 2,400
I'm not an expert on bit fields, but I thought C89 only allows bit fields of types unsigned int, signed int, or int; and that C99 added type _Bool. I wouldn't have thought type unsigned char was allowed.

As to your specific question, I agree that the three declarations are effectively equivalent because there is always at least one volatile in scope for every bit field for every case.
Sep 20 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
Many many compilers allow other types for bitfields as extensions so it is common to be able to use char, short or long based bitfields. That said bit fields are so platform dependent in implementation, order bits from the type are used in position and number of padding bits that I tend to stick clear of bitfield all together, especially as they are just syntactic sugar for the bitwise operators which you can use an write fully portable code.
Sep 20 '10 #3

alexis4
100+
P: 113
Thank you both for your answers!

Regards,
Alexis
Sep 21 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.