section 6.2: Structures and Functions

In this section, we'll begin playing with structures more or less as if they were ordinary variables such as we've been using all along (which they more or less are). As we'll see, we can declare variables of structure type, declare functions which accept structures as parameters and return them, declare pointers to structures, take the address of a structure (creating a pointer-to-structure) with &, and assign structures.

Notice that when we declare something as ``a structure type,'' we always have to say which structure type, usually by using the struct tag. If we've set up a ``point'' structure as above, then to declare a variable of this type, we say

	struct point thepoint;
Both
	struct thepoint;	/* WRONG */
and
	point thepoint;		/* WRONG */
would be errors.

The above list of things the language lets us do with structures lets us keep them and move them around, but there isn't really anything defined by the language that we can do with structures. It's up to us to define any operations on structures, usually by writing functions. (The addpoint function on page 130 is a good example. It will make a bit more sense if you think of it as adding not isolated points, but rather vectors. [We can't add Seattle plus Los Angeles, but we could add (two miles south, one mile east) plus (one mile east, two miles north).])

page 131

As an aside, how safe are the min() and max() macros defined at the top of page 131, with respect to the criteria discussed on pages 15 and 16 of the notes on section 4.11.2 (page 90 in the text)?

The precise meaning of the ``shorthand'' -> operator is that sp->m is, by definition, equivalent to (*sp).m, for any structure pointer sp and member m of the pointed-to structure.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback