/* Example 16.2: Pointers to Structures Author: Peter Brusilovsky */ #include #include struct point { float x; float y; }; struct point readpoint(char *prompt); double distance(struct point *p1, struct point *p2); void main(){ struct point pbase, p; int i; /* get base point */ pbase = readpoint("Enter base point"); /* get and check points */ for (i = 1; i <= 5; ++i) { p = readpoint("Enter point"); printf("Distance from base point is %.2f\n", distance(&pbase, &p)); } } /* readpoint: reads a point and returns it */ struct point readpoint(char *prompt) { struct point input; printf("%s (x y):", prompt); scanf("%f %f", &(input.x), &(input.y)); return input; } /* distance: returns distance between points */ double distance(struct point *pptr1, struct point *pptr2) { double xdiff, ydiff; xdiff = pptr1->x - pptr2->x; /* same as (*pptr1).x - (*pptr2).x */ ydiff = pptr1->y - pptr2->y; return(sqrt(xdiff*xdiff + ydiff*ydiff)); }