home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP11_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
30KB
|
653 lines
Chapter 11 - Structures and Unions
WHAT IS A STRUCTURE?
A structure is a user defined data type. You have the
ability to define a new type of data considerably more
complex than the types we have been using. A structure is a
combination of several different previously defined data
types, including other structures we have defined. An easy
to understand definition is, a structure is a grouping of
related data in a way convenient to the programmer or user
of the program. The best way to understand a structure is
to look at an example, so if you will load and display
STRUCT1.C, we will do just that.
The program begins with a structure definition. The
key word "struct" is followed by some simple variables
between the braces, which are the components of the
structure. After the closing brace, you will find two
variables listed, namely "boy", and "girl". According to
the definition of a structure, "boy" is now a variable
composed of three elements, "initial", "age", and "grade".
Each of the three fields are associated with "boy", and each
can store a variable of its respective type. The variable
"girl" is also a variable containing three fields with the
same names as those of "boy" but are actually different
variables. We have therefore defined 6 simple variables.
A SINGLE COMPOUND VARIABLE
Lets examine the variable "boy" more closely. As
stated above, each of the three elements of "boy" are simple
variables and can be used anywhere in a C program where a
variable of their type can be used. For example, the "age"
element is an integer variable and can therefore be used
anywhere in a C program where it is legal to use an integer
variable, in calculations, as a counter, in I/O operations,
etc. The only problem we have is defining how to use the
simple variable "age" which is a part of the compound
variable "boy". We use both names with a decimal point
between them with the major name first. Thus "boy.age" is
the complete variable name for the "age" field of "boy".
This construct can be used anywhere in a C program that it
is desired to refer to this field. In fact, it is illegal
to use the name "boy" or "age" alone because they are only
partial definitions of the complete field. Alone, the names
refer to nothing.
ASSIGNING VALUES TO THE VARIABLES
Using the above definition, we can assign a value to
each of the three fields of "boy" and each of the three
fields of "girl". Note carefully that "boy.initial" is
Page 77
Chapter 11 - Structures and Unions
actually a "char" type variable, because it was assigned
that in the structure, so it must be assigned a character of
data. Notice that "boy.initial" is assigned the character
'R' in agreement with the above rules. The remaining two
fields of "boy" are assigned values in accordance with their
respective types. Finally the three fields of girl are
assigned values but in a different order to illustrate that
the order of assignment is not critical.
HOW DO WE USE THE RESULTING DATA?
Now that we have assigned values to the six simple
variables, we can do anything we desire with them. In order
to keep this first example simple, we will simply print out
the values to see if they really do exist as assigned. If
you carefully inspect the "printf" statements, you will see
that there is nothing special about them. The compound name
of each variable is specified because that is the only valid
name by which we can refer to these variables.
Structures are a very useful method of grouping data
together in order to make a program easier to write and
understand. This first example is too simple to give you
even a hint of the value of using structures, but continue
on through these lessons and eventually you will see the
value of using structures.
Compile and run STRUCT1.C and observe the output.
AN ARRAY OF STRUCTURES
Load and display the next program named STRUCT2.C.
This program contains the same structure definition as
before but this time we define an array of 12 variables
named "kids". This program therefore contains 12 times 3 =
36 simple variables, each of which can store one item of
data provided that it is of the correct type. We also
define a simple variable named "index" for use in the "for"
loops.
In order to assign each of the fields a value, we use a
"for" loop and each pass through the loop results in
assigning a value to three of the fields. One pass through
the loop assigns all of the values for one of the "kids".
This would not be a very useful way to assign data in a real
situation, but a loop could read the data in from a file and
store it in the correct fields. You might consider this the
crude beginning of a data base, which it is.
In the next few instructions of the program we assign
new values to some of the fields to illustrate the method
Page 78
Chapter 11 - Structures and Unions
used to accomplish this. It should be self explanatory, so
no additional comments will be given.
A RECENT UPGRADE TO THE C LANGUAGE
Most modern C compilers will allow you to copy an
entire structure with one statement. This is a fairly recent
addition to the C language and will be a part of the ANSI
standard when it is published, so you should feel free to
use it with your C compiler if it is available. Line 22 is
an example of using a structure assignment. In this
statement, all 3 fields of kids[4] are copied into their
respective fields of kids[10].
WE FINALLY DISPLAY ALL OF THE RESULTS
The last few statements contain a "for" loop in which
all of the generated values are displayed in a formatted
list. Compile and run the program to see if it does what
you expect it to do.
USING POINTERS AND STRUCTURES TOGETHER
Load and display the file named STRUCT3.C for an
example of using pointers with structures. This program is
identical to the last program except that it uses pointers
for some of the operations.
The first difference shows up in the definition of
variables following the structure definition. In this
program we define a pointer named "point" which is defined
as a pointer that points to the structure. It would be
illegal to try to use this pointer to point to any other
variable type. There is a very definite reason for this
restriction in C as we have alluded to earlier and will
review in the next few paragraphs.
The next difference is in the "for" loop where we use
the pointer for accessing the data fields. Since