This is what I offered at an interview today.
int is_little_endian(void)
{
union {
long l;
char c;
} u;
u.l = 1;
return u.c == 1;
}
My interviewer insisted that c
and l
are not guaranteed to begin at the same address and therefore, the union should be changed to say char c[sizeof(long)]
and the return value should be changed to u.c[0] == 1
.
Is it correct that members of a union might not begin at the same address?