As the title indicates, i want to get the number of offset in bytes of a union member from other members before that member ( using the offsetof
macro defined in stddef.h
). This worked for structs as expected
#include <stdio.h>
#include <stddef.h>
struct bio {
int age;
char name;
};
int main( void ) {
printf("%d\n", offsetof(struct bio, name)); // result is 4 , which is what i expected
}
but for unions it printed 0
#include <stdio.h>
#include <stddef.h>
union bio {
int age;
char name;
};
int main( void ) {
printf("%d\n", offsetof(union bio, name)); // 0
}
Just a follow up question, the reason for this behavior is it because the members of a union are stored at the same block of memory ?
As par this explanation wikipedia offsetof i was also expecting to get a value of 4 not 0