1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>Chapter
6: Structures
</title>
10 <link href=
"sx8j.html" rev=precedes
>
11 <link href=
"sx9a.html" rel=precedes
>
12 <link href=
"top.html" rev=subdocument
>
15 <H1>Chapter
6: Structures
</H1>
18 </p><p>There's one other piece of motivation behind structures
19 that it's useful to discuss.
20 Suppose we didn't have structures
21 (or didn't know what they were or how to use them).
22 Suppose we wanted to implement payroll records.
23 We might set up a bunch of parallel arrays,
25 names, mailing addresses, social security numbers, and salaries
26 of all of our employees:
27 <pre> char *name[
100];
31 </pre>The idea here is that
32 <TT>name[
0]
</TT>,
<TT>address[
0]
</TT>,
<TT>ssn[
0]
</TT>, and
<TT>salary[
0]
</TT>
33 would describe one employee,
34 array slots with subscript
<TT>[
1]
</TT> would describe the second employee, etc.
35 There are at least two problems with this scheme:
36 first, if we someday want to handle more than
100 employees,
37 we have to remember to change the size of several arrays.
38 (Using a symbolic constant like
39 <pre> #define MAXEMPLOYEES
100
40 </pre>would certainly help.)
41 </p><p>More importantly,
42 there would be no easy way
43 to pass around all the information associated with a single employee.
44 Suppose we wanted to write the function
<TT>print_employee
</TT>,
45 which will print all the information associated with a particular employee.
46 What arguments would this function take?
47 We could pass it the index to use to retrieve the information from the arrays,
48 but that would mean that all of the arrays would have to be global.
49 We could pass the function an individual name, address, SSN, and salary,
50 but that would mean that
51 whenever we added a new piece of information to the database
52 (perhaps next week we'll want to keep track of employee's shoe sizes),
53 we would have to add another argument
54 to the
<TT>print_employee
</TT> function,
55 and change all of the calls.
57 the number of arguments to the
<TT>print_employee
</TT> function
58 would become unwieldy.)
59 What we'd really like is a way to encapsulate all of the data
60 about a single employee into a single data structure,
61 so we could just pass that data structure around.
62 </p><p>The right solution to this problem,
63 in languages such as C which support the idea,
64 is to define a structure describing an
66 We can make one array of these structures
67 to describe all the employees,
68 and we can pass around single instances of the structure where they're needed.
69 </p><p><a href=
"sx9a.html" rel=subdocument
>section
6.1: Basics of Structures
</a></p>
70 <p><a href=
"sx9b.html" rel=subdocument
>section
6.2: Structures and Functions
</a></p>
71 <p><a href=
"sx9c.html" rel=subdocument
>section
6.3: Arrays of Structures
</a></p>
72 <p><a href=
"sx9d.html" rel=subdocument
>The
<TT>sizeof
</TT> operator
</a></p>
73 <p><a href=
"sx9e.html" rel=subdocument
>section
6.4: Pointers to Structures
</a></p>
74 <p><a href=
"sx9f.html" rel=subdocument
>section
6.5: Self-referential Structures
</a></p>
78 <a href=
"sx8j.html" rev=precedes
>prev
</a>
79 <a href=
"sx9a.html" rel=precedes
>next
</a>
80 <a href=
"top.html" rev=subdocument
>up
</a>
81 <a href=
"top.html">top
</a>
84 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
85 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
86 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>