/* struct.c */ /* a slightly less silly C program, whose functionality however is essentially identical to plainInts.c. The one significant difference is that the program wraps the simple coordinate values in what C calls a "struct" (cf. Pascal "record", etc.). "Struct" is C's provision for user-defined data types--types relevant to the problem domain of a program, composed of primitive C types and/or previously defined structs */ #include /* the syntax here is "struct { [list of component types and tags for them] }"; in this case we're defined an unnamed type consisting of two integers, and declared a (global) variable of the type called my_pt struct { int x, y; } my_pt; void setPoint(int xVal, int yVal); /* observe that the syntax for referring to components of structs is [struct-variable].[member] */ int main() { my_pt.x = my_pt.y = 500; printf("After init my_pt.x is: %d, my_pt.y is: %d\n", my_pt.x, my_pt.y); setPoint(800, 1400); printf("After setPoint() my_pt.x is: %d, my_pt.y is: %d\n", my_pt.x, my_pt.y); } void setPoint(int xVal, int yVal) { my_pt.x = xVal; my_pt.y = yVal; }