let us consider following code
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct bf_{
unsigned x:4;
unsigned y:4;
unsigned z:4;
unsigned w:4;
}bf;
int main(){
unsigned short i=8;
unsigned short j=9;
bf* bitfields=(bf *)&i;
bf*bit=(bf*)&j;
bitfields->w=12;
printf("%d\n",bitfields->x);
printf("%d\n",bit->y);
printf("%d\n",bitfields->w);
return 0;
}
this fragment
unsigned short j=9;
bf*bit=(bf*)&j;
printf("%d\n",bit->y);
i have add after guess some interesting characteristic of this code for example after this place
bf* bitfields=(bf *)&i;
when we write printf("%d\n",bitfields->x);
which prints 8 i understand that using pointers and references value of i will be granted to x and so it prints 8 for instance when we write bitfiled->y
it writes 0 so i decided to introduce second element variable j create new instance of bf structer and make reference of j after which
statement bit->y should write 9 because as i understand it is order definition but it gives me 0 why? please explain me how this code works?i am not english speaker so please sorry for my bad english