section 6.1: Basics of Structures

Don't get too excited about the prospect of doing graphics in C--there's no one standard or portable way of doing it, so the points and rectangles we're going to be discussing must remain abstract for now (we won't be able to plot them out).

page 128

To summarize the syntax of structure declarations: A structure declaration has about four parts, most of them optional: the keyword struct, a structure tag (optional), a brace-enclosed list of declarations for the members (also called ``fields'' or ``components'') of the structure (optional), and a list of variables of the new structure type (optional). The arrangement looks like this:

	struct tag {
		member declarations
	} declared variables ;
Normally, a structure declaration defines either a tag and the members, or some variables based on an existing tag, or sometimes all three at once. That is, we might first declare a structure:
	struct point {			/* 1 */
		int x;
		int y;
	};
and then some variables of that type:
	struct point here, there;	/* 2 */
Or, we could combine the two:
	struct point {			/* 3 */
		int x;
		int y;
	} here, there;
The list of members (if present) describes what the new structure ``looks like inside.'' The list of variables (if present) is (obviously) the list of variables of this new type which we're defining and which the rest of the program will use. The tag (if present) is just an arbitrary name for the structure type itself (not for any variable we're defining). The tag is used to associate a structure definition (as in fragment 1) with a later declaration of variables of that same type (as in fragment 2).

One thing to beware of: when you declare the members of a structure without defining any variables, always remember the trailing semicolon, as shown in fragment 1 above. (If you forget it, the compiler will wait until the next thing it finds in your source file, and try to define that as a variable or function of the structure type.)


Read sequentially: prev next up top

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