I am trying to make sure I got the referencing of structures, especially nested structure variables understood. Please look over the following declared structures and let me know if my answer to a - f questions are on target or not. Thanks.
Given the following:
struct point { int x, y;};
struct shape {
int shape_kind; /* RECTANGLE or CIRCLE */
struct point center; /* coordinates of center */
union {
struct {
int length, width;
} rectangle;
struct {
int radius;
} circle;
} u;
} s;
Which of the following are legal AND safe?
Which are legal though maybe unsafe?
Which are illegal ?
(a) s.shape_kind = RECTANGLE /* legal and safe */
(b) s.center.x = 10; /* legal and safe */
(c) s.length = 25; /* not legal */
should be :
s.u.rectangle.length = 25;
(d) s.u.rectangle.width = 8; /* legal and safe */
(e) s.u.circle = 5; /* legal but maybe not safe */
(f) s.u.radius = 5; /* not legal */
should be :
s.u.circle.radius = 5;
I would appreciate a quick response as I am being tested today over this.
Thanks.