/* Demonstrate usages of pointer type by exmaples of a point and a rectangle type in 2D plane Emad Arasteh December 5 , 2019 */ #include typedef struct { int x; int y; } point_t; typedef struct { point_t coords[4]; } rectangle_t; void modify(point_t *p, int v, int w) { p->x = v; p->y = w; } void display(rectangle_t* rect) { int i; printf("Coordinates of the rectangle are:\n"); for(i = 0; i< 4; i++) { printf(" (X,Y) : (%d, %d)\n", rect->coords[i].x, rect->coords[i].y); } } int main() { point_t p1, *p2 = &p1; rectangle_t r1 = { { {6, 8}, {6, 14}, {8, 8}, {8, 14} } }; rectangle_t *r2 = &r1; p1.x = 6; p1.y = 9; printf("P1(X,Y) : (%d, %d)\n", p1.x, p1.y); /* 1. pass point_t variable by reference instead of by value */ modify(p2, 42, 24); printf("P1(X,Y) : (%d, %d)\n", p1.x, p1.y); /* 2. avoid copying the entire rectangle_t struct by passing only a pointer */ display(r2); return 0; }