Difference between struct and union

union foo {
  int a;   // can't use both a and b at once
  char b;
} foo;
struct bar {
  int a;   // can use both a and b simultaneously
  char b;
} bar;
union foo x;
x.a = 3; // OK
x.b = 'c'; // NO! this affects the value of x.a!
struct bar y;
y.a = 3; // OK
y.b = 'c'; // OK


Leave a Reply

Your email address will not be published. Required fields are marked *