structured data types

Structure Data Type


Composite data types are language components which can hold more than a single data value at the same time. The most common form of composite data type is the array; however, the array orlist data type is somewhat restrictive in as much as every data value within a variable of a given array must be of the same type (it is possible to have an array of int values, or an array of float values, but not an array of int and float values).
Sometimes it is useful to be able to collect several different data values (often of different types) together as a single item. One of the most common examples of this is the record structure, used to "encapsle" multiple pieces of information about an individual (person, event, thing). Coding this type of structure requires something a bit more complex than a simple array or list of a single data type sharing a common name.

Declaring A Record Structure Data Type

In order to work with composite structured data, it is first necessary to describe what the structure of this particular data looks like (since it is not a simple, built-in data type). The structure being described must be given a "name" (so that, later on, variables can be defined using this new type), and the components which make up its internal structure must be identified. The sub-structuralcomponents will normally be basic, built-in data types; each must be identified with its own name and with its data type.
The form for describing a structure, called the structure declaration is composed of:
  1. the keyword struct
  2. the name to be used for this new structure data type
  3. a left brace bracket ({)
  4. a list of the component declaration statements, each composed of:
        the component's data type;
        followed by the name of the component within the structure;
        (followed by an array size, in square brackets, if the component is an array);
        and terminated with a semi-colon (;)
  5. a terminating right brace bracket (})
For example, a structure declaration for a new data type which could be used to describe a room within an institution, such as a college building, might look like:      struct roomrec
      {
       char roomNum[7]; /* null-terminated char array */
       char roomType;
       int capacity;
      }

Notice that although the "component declarations" within a structure declaration look like variable definition statements, they are not variable definition statements. No space is reserved to hold data by these statements; they are simply "descriptions" or declarations of what each "component" should look like, if a variable of this new structure type were defined. As a particular note, since no actual space is being set aside for these components within the structure declaration, it is impossible to assign initial values to such structural components; for example:
   (within the structure declaration above)
       int capacity = 0;
is not valid!

Structure declarations which describe a new composite data type which might be commonly used by a collection of programs within some larger system are often stored in a separate "header" (.h) files which can then be included in each program which requires variables of that type.

Defining Variables of A Declared Record Structure

After a specific structure data type has been declared, it is possible to define variables of this new type. The syntax for a structure variable definition is basically the same as for any other type of variable.
For example, assuming the roomrec structure declaration (above), it is possible to define a variable, classRoom, as:
      roomrec classRoom;
      (in some old versions of the C-language it was necessary to code this as struct roomrec classRoom;)
Notice that structure variable definitions can not be coded until after the code for the structure declaration.

Referencing Elementary Values Within A Record Structure Variable

While structure variables are often read from or written to files as single, composite data items, most of the processing is performed on the sub-structure elementary data fields. To work with a "sub-structure elementary data field" it is necessary to specify both:
   the name of the structure variable, and
   the component name from within the structure declaration.
These two names are combined into a single name, first the "variable name" and then the "component name", separated by a period (.); as with any name in C++, there can be no spaces in this two part name.

Using the examples from above:
  structure declaration:
      struct roomrec
      {
       char roomNum[7]; /* null-terminated char array */
       char roomType;
       int capacity;
      }

  and variable definition:
      roomrec classRoom;
then if we wanted to check if the room described by some data in the classRoom record structure had a capacity of more than 40 people, we would code:
      if (classRoom.capacity > 30) ....


Nested Record Structures

Sometimes it is desireable to code a sub-structure component as being a structure itself, instead of a simple built-in data type. This is permissible. For example, it is often desireable to always use the same format for all dates within a system; since there is no built-in type date, the easiest way to accomplish this is to declare a new structured data type:
      struct dayt
      {
         int year;
         int month;
         int day;
      }

  (notice the unusual spelling "dayt" since "date" is a reserved word in C++).

The structure of an employee record for some company, might then use this new structure type (possibly multiple times):
      struct employee
      {
         int employeeNumber;
         // (other field declarations)
         dayt dateHired;
         // (some more field declarations)
         dayt dateOfBirth;
         // (etc.)
      }

if a record structure variable were defined, for example as:
      employee janitorStaff;
and this, janitorStaff, record had data "read" into it,

the year in which the employee, whose information was currently in the janitorStaff record, was hired could be displayed using:
      cout << janitorStaff.dateHired.year;

No comments:

Post a Comment