#include #define PI 3.14159265 /* Define a structure named point - and define a type named "point" which ** is the same as "struct point" */ typedef struct point { int x; int y; } point; /* We don't need to name the structure when using a typedef */ typedef struct { point ll; /* lower left */ point ur; /* upper right */ } rectangle; struct circ { point centre; int radius; }; /* The typedef can be separated from the structure definition and need ** not have the same name*/ typedef struct circ circle; /* Function prototypes */ void make_point(point* p, int x, int y); void make_rectangle(rectangle* r, point lower_left, point upper_right); int rectangle_area(rectangle r); double circle_area(circle c); int is_square(rectangle r); int main() { /* It is possible to initialise structures when they are created (in a ** similar fashion to the way arrays can be initialised). We can refer ** to points as "point" or "struct point" */ point a = {1, -1}; struct point b = {5, 3}; point p; rectangle r1 = { {2, 2}, {5,6} }; rectangle r2; circle c1; make_point(&p, -1, -1); /* Modified the way this is called */ c1.centre = p; /* Structures can be copied */ c1.radius = 4; make_rectangle(&r2, a, b); /* Modified the way this is called */ printf("Rectangle 1: (%d,%d) to (%d,%d). Area is %d\n", r1.ll.x, r1.ll.y, r1.ur.x, r1.ur.y, rectangle_area(r1)); printf("Rectangle 2: (%d,%d) to (%d,%d). Area is %d\n", r2.ll.x, r2.ll.y, r2.ur.x, r2.ur.y, rectangle_area(r2)); printf("Circle: centred at (%d,%d), radius %d. Area is %g\n", c1.centre.x, c1.centre.y, c1.radius, circle_area(c1)); } void make_point(point *p, int x, int y) { p->x = x; p->y = y; } void make_rectangle(rectangle *r, point ll, point ur) { r->ll = ll; r->ur = ur; } int rectangle_area(rectangle r) { return (r.ur.y - r.ll.y) * (r.ur.x - r.ll.x); } double circle_area(circle c) { /* Note that because we multiply integers by a double, the result will ** be a double */ return (PI * c.radius * c.radius); }