I have this code - http://ideone.com/sXhWxf
#include <stdio.h>
int main(void) {
struct bitfield{
unsigned a:5;
unsigned c:5;
unsigned b:6;
} bit = {1,3,3};
char *p = (char*)&bit;
printf("%d\n",*p);
p++;
printf("%d\n",*p);
// I assumed that the bits are laid out in the below order in the memory.
// Spaces are just for clarity
// 00001 00011 000011
// Also, I asumed that the 'char' will take 8 bits. But I can't understand output.
// According to me the output should be - 8 195 (considering the 1st 8 bits &
// last eight bits for the printf statements)
return 0;
}
The output is -
97
12
Can someone help me understand this output in detail? (Please read comments in the code)
Also, I came across this statement on Wikipedia which says "The members of bit fields do not have addresses, and as such cannot be used with the address-of (&) unary operator. The sizeof operator may not be applied to bit fields." But I am able to access the address of 'bit' variable. How is that? Am I not interpreting the statement correctly? Please guide me.