![]() ![]() | structures/point-rect-2.c - Et udvidet eksempel på structures i structures. | Lektion 12 - slide 14 : 36 Program 4 |
#include <stdio.h> struct point { int x, y; }; struct rectangle { struct { int x, y; } p1, p2; }; int main(void) { struct rectangle r; struct point pt1, pt2; /* OK - initialization of x,y coordinates of points in rectangles */ r.p1.x = 1; r.p1.y = 2; r.p2.x = 10; r.p2.y = 12; /* Not OK - cannot assign points to the fields of rectangle */ pt1.x = 1; pt1.y = 2; pt2.x = 10; pt2.y = 12; r.p1 = pt1; /* error: incompatible types */ r.p2 = pt2; /* error: incompatible types */ return 0; }