I have a struct of the following type
typedef struct
{
unsigned int a : 8;
unsigned int b : 6;
unsigned int c : 2;
}x, *ptr;
What i would like to do, is change the value of field c.
I do something like the following
x structure = { 0 };
x->c = 1;
When I look at the memory map, I expect to find 00 01, but instead I find 00 40. It looks like when arranging the second byte, it puts c field in the lowest bits and b field in the highest bits. I've seen this on both GCC and Windows compilers.
For now, what I do is the following, which is working OK.
unsigned char ptr2 = (unsigned char*) ptr
*(ptr2 + 1) &= 0xFC
*(ptr2 + 1) |= 0x01
Am I looking at the memory map wrong? Thank you for your help.
ptr
holds&structure
(which is not clear in your question)*(ptr+1)
is a quick walk into undefined behavior.*ptr
as a type andptr
as a variable tossed my head. I think i see what you're doing.ptr
is (now was) anunsigned char*
in the dereference code.