// *** before: ***
//C99: a union inside a struct needed a member name,
//and every access went through it
struct ShapeOld {
int kind;
union {
double radius;
struct {
double width;
double height;
} rect;
} u;
};
struct ShapeOld boxOld;
boxOld.kind = 1;
boxOld.u.rect.width = 3;
boxOld.u.rect.height = 4;
double areaOld = boxOld.u.rect.width * boxOld.u.rect.height;
//areaOld is 12
printf("areaOld is %g\n", areaOld);
// *** in version C11: ***
//a member without a name: its fields belong to the outer struct
struct Shape {
int kind;
union {
double radius;
struct {
double width;
double height;
};
};
};
struct Shape box = {.kind = 1, .width = 3, .height = 4};
double area = box.width * box.height;
//area is 12
printf("area is %g\n", area);