Neat trick but just skirts initialization best practices. You're always supposed to initialize structures directly (otherwise use a function, like memset).Like this?
[...]
Output:
different
Code:
#include <string.h>
#include <stdio.h>
#pragma packed
typedef union
{
struct
{
unsigned a:1; unsigned b:1; unsigned c:1; unsigned d:1; unsigned e:1; unsigned f:1; unsigned g:1; unsigned h:1; unsigned i:1; unsigned j:1; unsigned k:1;
unsigned Reserved:22;
} MyBits;
unsigned int FourBytes;
} MyUnion;
int main(int argc, char* argv[])
{
MyUnion c1 = {0};
MyUnion c2 = {0};
if (memcmp(&c1, &c2, sizeof(MyUnion)) == 0)
{
printf("same\n");
}
else
{
printf("different\n");
}
return 0;
}
// Output: "same"