advanced techniques. In general, you’ll find structures to be useful whenever information
of different variable types should be treated as a group. For example, in a mailing list
database, each entry could be a structure, and each piece of information (name, address,
city, and so on) could be a structure member.
Listing 11.1 pulls together everything that has been covered up to this point. It is not
very practical; however, it illustrates the point of a simple structure.
LISTING11.1 simple.c. Declaring and using a simple structure
1: /* simple.c - Demonstrates the use of a simple structures*/
2:
3: #include <stdio.h>
4:
5: int length, width;
6: long area;
7:
8: struct coord{
9: int x;
10 int y;
11: } myPoint;
12:
13: int main( void )
14: {
15: /* set values into the coordinates */
16: myPoint.x = 12;
17: myPoint.y = 14;
18:
19: printf(“\nThe coordinates are: (%d, %d).”,
20: myPoint.x, myPoint.y);
21:
22: return 0;
23: }
The coordinates are: (12, 14).
This listing defines a simple structure for holding the coordinates of a point. This
is the same structure you’ve seen illustrated earlier in today’s lessons. In line 8
you see that structkeyword is used followed by the tag,coord. The body of the struc-
ture is then defined in lines 9 to 11. This structure is declared with two members,xandy
which are both variables of type int.
In line 11, a variable called myPointis also declared as an instance of the coordstruc-
ture. This declaration could also have been done in a separate line as follows:
struct coord myPoint;
252 Day 11
INPUT/
OUTPUT
ANALYSIS
18 448201x-CH11 8/13/02 11:17 AM Page 252