Here's another example with nested structures that shows
/*************************************** ** File: euclid.c ** Author: Tim Finin ** Date: 03/08/05 ** email: finin@cumbc.edu ** Modified: 9/25/05 ** By: Sue Evans ** Modified to meet current 201 standards ** ** This program verifies that assigning ** one structure to another in C *copies* ** all of the values. It is not like assignment ** for objects. ** *************************************/ #include <stdio.h> /* structures */ /* a 2D point has an x and y coordinate */ struct point { int x; int y; }; /* a line has a start and end point */ struct line { struct point startPoint; struct point endPoint; }; /* function prototypes */ void PrintGreeting (void); struct line ReverseLine(struct line theLine); void PrintLine (struct line theLine); int main (void) { PrintGreeting(); struct point p1 = {-1, -2}; struct point p2 = {1, 2}; struct line line1; struct line line2; printf("Initial values: p1 is (%d,%d), p2 is (%d,%d)\n", p1.x, p1.y, p2.x, p2.y); p1 = p2; printf("After p1=p2: p1 is (%d,%d), p2 is (%d,%d)\n", p1.x, p1.y, p2.x, p2.y); p2.x = 100; p2.y = 200; printf("After modifying p2: p1=(%d,%d), p2=(%d,%d)\n", p1.x, p1.y, p2.x, p2.y); p1.x = -100; p1.y = -200; printf("After modifying p1: p1=(%d,%d), p2=(%d,%d)\n", p1.x, p1.y, p2.x, p2.y); printf("\nLines...\n"); line1.startPoint = p1; line1.endPoint = p2; line2 = ReverseLine(line1); printf("line1:"); PrintLine(line1); printf(" line2:"); PrintLine(line2); printf("\n"); line1 = line2; printf("After line1 = line2, line1:"); PrintLine(line1); printf(" line2:"); PrintLine(line2); printf("\n"); line2.endPoint.x = 999; printf("After line2.endPoint.x = 999,\nline1:"); PrintLine(line1); printf(" line2:"); PrintLine(line2); printf("\n"); return 0; } /*************************************** ** PrintLine prints a line w/o newlines ** ** INPUTS: a line structure. ** OUTPUT: void ** *************************************/ void PrintLine (struct line l) { printf("(%d,%d)=>(%d,%d)", l.startPoint.x, l.startPoint.y, l.endPoint.x, l.endPoint.y); } /*************************************** ** ReverseLine reverses a line struct, i.e., ** the start and end points are swaped. ** ** INPUTS: a line structure. ** OUTPUT: a line structure ** *************************************/ struct line ReverseLine(struct line l) { struct line reversed; reversed.startPoint = l.endPoint; reversed.endPoint = l.startPoint; return reversed; } /*************************************** ** PrintGreeting prints an initial greeting message ** and explanation of the program ** ** INPUTS: void ** OUTPUT: void ** *************************************/ void PrintGreeting (void) { printf("Euclidian fun\n\n"); printf("This example uses both struct points and struct lines, making\n"); printf("use of nested structures to model points & line segments.\n"); printf("It also shows functions that take structs as arguments and\n"); printf("that return structs.\n\n"); }
% a.out Euclidian fun This example uses both struct points and struct lines, making use of nested structures to model points & line segments. It also shows functions that take structs as arguments and that return structs. Initial values: p1 is (-1,-2), p2 is (1,2) After p1=p2: p1 is (1,2), p2 is (1,2) After modifying p2: p1=(1,2), p2=(100,200) After modifying p1: p1=(-100,-200), p2=(100,200) Lines... line1:(-100,-200)=>(100,200) line2:(100,200)=>(-100,-200) After line1 = line2, line1:(100,200)=>(-100,-200) line2:(100,200)=>(-100,-200) After line2.endPoint.x = 999, line1:(100,200)=>(-100,-200) line2:1(100,200)=>(999,-200) %