2

Please help me out with logic associated with code snippet below. The output I get is 12 8. How come *p is 12? I understood its size to be 8, but I'm perturbed with value displayed for *p.

int main()    
{    
    struct bitfield
    {
        unsigned a:5;
        unsigned c:5;
        unsigned b:6;
    } bit;

    char *p;

    struct bitfield bit1={1,3,3};

    p=(char*)&bit1;

    p++;

    printf("%d\t%d",*p,sizeof(bit1));

    getchar();

    return 0;
}
1
  • Note that almost everything to do with bit-fields w.r.t memory layout and the like is implementation defined. Accessing the bit-field values via the char * is implementation defined at best. It would certainly be feasible for the value displayed to be all zero on some types of processor. Commented Dec 19, 2015 at 17:20

2 Answers 2

3

Firstly,

because of the declaration

struct bitfield
{
    unsigned a:5;
    unsigned c:5;
    unsigned b:6;
}bit;

Your bit-field bit will have 16-bit bitfield with the arrangement like this

00000000 00000000
ccccccbb bbbaaaaa

Secondly, because the input is {1, 3, 3}, so the 1 will be assigned to a, 3 to b, and another 3 to c, makes it become like this

00001100 01100001
ccccccbb bbbaaaaa

Thirdly, because the p is just character pointer, it will originally take the last byte (an 8-bit) among the two, which has smaller address in little endian format (edit: albeit implementation/platform dependent, see comment and another answer from Abstraction), so, p points to

Additional note from MDSN

The 8086 family of processors stores the low byte of integer values before the high byte

01100001
bbbaaaaa

But fourthly, you increase the p! Thus, in little endian format, it now points to

00001100
ccccccbb

Then finally, you print the value of what is pointed by p. And obviously, 00001100 is nothing but bit representation of decimal value 12. That is why you get 12.

Sign up to request clarification or add additional context in comments.

3 Comments

nicely explained. Thanks Ian.
No problem, I am glad that you find it useful. It once confused me too. =)
Note that this layout is, like almost everything to do with bit-fields, implementation specific. The layout, packing, etc of the bits depends on the implementation.
3

This is very platform/implementation dependent code. Assuming that the values are tightly packed in 16bit integer and the endianness is little endian then your values should look like (left -> right | lower memory address -> higher memory address)

   1     3     3
  ||     ||    ||
10000 110 00 110000
     ^   ^  ^
     5   8  10

Now 00110000 is 12 (the notation is from left to right least to most significant and 00001100 in the standard notation). And your p points after the first 8 bits since you increment it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.