In the programs written so far, each variable was associated with a single memory location. These variables are called scalar variables, and their data types are scalar or unstructured types. In this chapter, we will begin the study of composite types. A composite type is one which defines a collection of related data values. The items in a variable of a composite type can be processed individually, although some operations may be performed on the structure as a whole.
Ada provides type constructors, which can be used to form composite
types from simpler types. The type constructors RECORD
and
ARRAY
are introduced in this chapter and some simple cases are
explored. More complex and interesting uses of arrays and records are taken up
beginning in Chapter 10.
A record is a data structure containing a group of related data items; the individual components, or fields, of a record can contain data of different types. An array is a data structure used for storage of a collection of data items that are all of the same type. An array has elements; each element has a position within the array, known as its index or subscript.
Think of a single record as analogous to a 3 by 5 file card containing,
e.g., the name, address, birthday, and phone number of one of your friends.
Each card has the same structure--a collection of fields, each field having its
own type. A record type declaration, then, is a way to describe this record
structure. Each data item is stored in a separate record field; we can
reference each data item stored in a record through its field name. For
example, Person.Name
references the field Name
of the
record Person
.
Think of an array as analogous to the box in which you keep your set of
cards. An array type declaration describes the structure of the array. If the
array is called Friends
, then one of the friend records might be
referenced as Friends(37)
, the card in the thirty-seventh position
of the array.
Records and arrays make it easier to organize and represent information in Ada and other modern programming languages, and these composite types are an important contributor to the power of these languages to enable us to write complex programs.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.