// *** было: ***
//C99: объединению внутри структуры нужно было имя члена,
//и каждый доступ шёл через него
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);
// *** в версии C11: ***
//член без имени: его поля принадлежат внешней структуре
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);