#include #include struct Point { double x; double y; }; // defining a structure typedef struct Point point; // defining a new type using structure point new_point(double c, double d) // structure as return value { point p; p.x = c; p.y = d; return p; } double distance(point a, point b) // structure as parameter { double d = 0.0; d = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); return d; } void modify_wrong(point p, double c, double d) { p.x = c; // modifying members inside function is temporary p.y = d; } void modify_pointer(point *p, double c, double d) { p->x = c; // modifying members using structure pointer is permanent p->y = d; // -> notation } int main() { struct Point p, q; // declaring using structure point s; // declaring using type point t = {9.0, -5.0}; // initializing during declaration double d; point *ptr; printf("%lf %lf\n", p.x, p.y); // by default, values are 0 q.x = 4.0; // accessing or modifying the members in a structure q.y = -3.0; // . notation d = distance(p, q); printf("Distance = %lf\n", d); //p = {9.0, -5.0}; // error //printf("%lf %lf\n", p.x, p.y); p = new_point(7.0, -1.0); printf("%lf %lf\n", p.x, p.y); modify_wrong(q, 7.0, -1.0); printf("%lf %lf\n", q.x, q.y); ptr = &p; modify_pointer(ptr, 2.0, -5.0); printf("%lf %lf\t%lf %lf\n", p.x, p.y, ptr->x, ptr->y); printf("Size of p is %d, size of ptr is %d\n", sizeof(p), sizeof(ptr)); //if (q == p) // error // printf("Equal structures\n"); }