Structures or Record Types

The types of the individual values characterising an object of structured type, are in general different. For example, Person may be characterized by Age and Sex: one is an integer, the other a character.

The definition of this type would be:

struct person {
                int Age;
                char Sex;
              }
"age" and"sex" define two fields of the structure.

Each field holds information of a different kind. Fields can of course have the same type, but still represent essentially distinct kinds of information.

struct parent {
                int Age;
                int NumberofChildren;
              }
If a variable is defined to be of a struct type, the individual values in the different fields can be selected by appending the name of the field to the name of the variable e.g. for a variable "Student" declared to be of type person,
    struct person Student;
we should use the following identifiers to refer to the first and second fields of the record:
    Student.age
    Student.sex
When "Student" alone is used in a program, it refers to the complete record. This is usually only the case when a structure is passed as a parameter or returned as the result of a function, although it is possible to copy a whole structure's contents into another by using the assignment operator.

Exercises on this section.


Next - Unions.

Back to Contents page.