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:
#ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED
#define ANON
#else
#define ANON3(X) anonymous__## X ##__
#define ANON2(X) ANON3(X)
#define ANON ANON2(__LINE__)
#endif
struct union_struct {
char foo;
union {
struct {
char bar : 2;
char ANON : 6;
};
struct {
char ANON : 2;
char foobar1 : 6;
};
struct {
char ANON : 2;
char foobar2 : 6;
};
};
};
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 allow foobar1
and foobar2
to represent the same 6 bits that follow bar
.
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 each ANON
macro into a name for the bit-field.
However, the C.11 standard only defines bit-fields on _Bool
, int
, and unsigned
, while the use of any other type for a bit-field is implementation defined (C.11 §6.7.2.1 ¶5).