[ Pobierz całość w formacie PDF ]

structure contains two simple variables, grade and a string named
lunch[25], and the structure named descrip. Since descrip contains
three variables, the new structure actually contains 5 variables. This
structure is also given a name, alldat, which is another type definition.
Finally we define an array of 53 variables each with the structure
defined by alldat, and each with the name student. If that is clear, you
will see that we have defined a total of 53 times 5 variables, each of
which is capable of storing a value.
Since we have a new type definition we can use it to define two more
variables. The variables teacher and sub are defined in line 13 to be
variables of the type alldat, so that each of these two variables contain
5 fields which can store data.
Using fields
In the next five lines of the program we assign values to each of the
fields of teacher. The first field is the grade field and is handled just like
the other structures we have studied because it is not part of the nested
structure. Next we wish to assign a value to her age which is part of the
nested structure. To address this field we start with the variable name
teacher to which we append the name of the group descrip, and then
we must define which field of the nested structure we are interested in,
so we append the name age. The teacher s status field is handled in
exactly the same manner as her age, but the last two fields are assigned
strings using the string copy function strcpy, which must be used for
string assignment. Notice that the variable names in the strcpy function
are still variable names even though they are each made up of several
parts.
The variable sub is assigned nonsense values in much the same way, but
in a different order since they do not have to occur in any required
Page 813-24
Module 815 Data Structures Using C
order. Finally, a few of the student variables are assigned values for
illustrative purposes and the program ends. None of the values are
printed for illustration since several were printed in the last examples.
It is possible to continue nesting structures until you get totally
confused. If you define them properly, the computer will not get
confused because there is no stated limit as to how many levels of
nesting are allowed. There is probably a practical limit of three beyond
which you will get confused, but the language has no limit. In addition
to nesting, you can include as many structures as you desire in any level
of structures, such as defining another structure prior to alldat and
using it in alldat in addition to using person. The structure named
person could be included in alldat two or more times if desired.
Structures can contain arrays of other structures which in turn can
contain arrays of simple types or other structures. It can go on and on
until you lose all reason to continue. Be conservative at first, and get
bolder as you gain experience.
Exercise 4
Define a named structure containing a string field for a name, an integer
for feet, and another for arms. Use the new type to define an array of
about 6 items. Fill the fields with data and print them out as follows:
A human being has 2 legs and 2 arms.
A dog has 4 legs and 0 arms.
A television set has 4 legs and 0 arms.
A chair has 4 legs and 2 arms.
etc.
2. Rewrite the previous exercise using a pointer to print the data out.
Page 813-25
Module 815 Data Structures Using C
Objective 5 After working through this module you should be able to use unions to
define alternate data sets for use in C programs.
Unions
A union is a variable that may hold (at different times) data of different
types and sizes. For example, a programmer may wish to define a
structure that will record the citation details about a book, and another
that records details about a journal. Since a library item can be neither a
book or a journal simultaneously, the programmer would declare a
library item to be a union of book and journal structures. Thus, on one
occasion item might be used to manipulate book details, and on another
occassion, item might be used to manipulate journal details. It is up to
the programmer, of course, to remember the type of item with which
they are dealing.
Examine the next program [UNION1.C] for examples.
void main( )
{
union {
int value; /* This is the first part of the union
*/
struct {
char first; /* These two values are the second part
*/
char second;
} half;
} number;
long index;
for (index = 12; index
number.value = index;
printf("%8x %6x %6x\n", number.value, number.half.first,
number.half.second);
}
} [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szkla.opx.pl
  •