Is it possible in C to get the size of the following structure to be 2?
#include <stdio.h>
struct union_struct {
char foo;
char bar : 2;
union {
char foobar1 : 6;
char foobar2 : 6;
};
};
int main(void)
{
printf("size of union struct: %d\n", sizeof(struct union_struct));
return 0;
}
output, compiled with gcc:
size of union struct: 3
If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently:
The first byte is
foo
, the second byte is the anonymous union. Then anonymous union has 3 single byte anonymous structs. Each struct (optionally) uses unnamed bit-fields to allowfoobar1
andfoobar2
to represent the same 6 bits that followbar
.From my understanding of the C.11 standard the above code is correct when
UNNAMED_BITFIELDS_ARE_WELL_DEFINED
is defined. However, there seems to be debate on whether or not unnamed bit-fields have well-defined semantics (see comments below). If unnamed bit-fields do not have well-defined semantics, then the code above can expand eachANON
macro into a name for the bit-field.However, the C.11 standard only defines bit-fields on
_Bool
,int
, andunsigned
, while the use of any other type for a bit-field is implementation defined (C.11 §6.7.2.1 ¶5).