Code
#include <stdio.h>
struct test {
unsigned int x; // consider int as 4 bytes
long int y : 33; // consider long as 8 bytes
unsigned int z;
};
int main()
{
struct test t;
unsigned int* ptr1 = &t.x;
// long int* ptr3 = &t.y;
unsigned int* ptr2 = &t.z;
printf("sizeof(struct test) %lu\n", sizeof(struct test));
printf("&t.x %u\n", ptr1);
printf("&t.z %u\n", ptr2);
printf("ptr2 - ptr1 %ld\n", ptr2 - ptr1);
return 0;
}
Output:
I was expecting
- 4 bytes to be assigned for x
- since the long int is bitfielded, 33 bits -> 4 + 4 bytes are assigned to it but only 33 is used
- 4 bytes are then assigned to z and doesn't take up the remaining space in the previous 8 bytes because there is no 4 complete bytes left.
A total of 16 bytes and somehow it is 24
How does that work?
Also since order is important in bitfields, if x starts at 3187370912, since it is not a bitfield it gets 4 bytes. The long int gets 8 bytes, even though 33 bits are only used, since the next field is not a bitfield, so it takes additional 4 bytes. So the ptr between x and z should have 4+8 = 12 bytes difference. But the output is 4 bytes.
How does that work?

4indicates16bytes.struct test {unsigned int x; unsigned int z; long int y : 33;};it will be of size 16 because of byte paddingprintf("sizeof(struct test) %zu\n", sizeof(struct test)); printf("&t.x %p\n", (void*) ptr1); printf("&t.z %p\n", (void*) ptr2); printf("ptr2 - ptr1 %td\n", ptr2 - ptr1);.