« previous  

5.4    Derived Data Types

    You have seen in previous chapters the six intrinsic data types of Fortran 90: INTEGER, REAL, CHARACTER, COMPLEX, LOGICAL and arrays.  Some times it is useful to define new data types, or derived data types.  New types are define using the TYPE command:

    TYPE name
    END TYPE name

The declarations define the components in this type.  It declares both the name and type of each component.  For example:

    TYPE course_list
        CHARACTER (15) :: Last_name, First_name
        CHARACTER (1) :: Middle
        INTEGER :: Student_ID
        REAL :: average
        CHARACTER(1) :: grade
    END TYPE course_list

Variables or constants can be defined as a certain type using the following declaration:

    TYPE(name) :: var

Using the type defined above:

    TYPE(course_list) :: A, B

Now A and B will have 6 components each:  Last_name, First_name, Middle, Student_ID, average, grade, and list.  You can also define an array so that all its components are of derived type:

    TYPE(course_list), DIMENSION(10) :: A

Now A is a single dimension array with 10 elements all of the derived type course_list.  We can assign a value to an element of A as follows:

    A(1) = couse_list("Smith", "Joe", "J", 11111, 95.5, "A")

The first element of A contains all this information.  What if you want to access only a portion of A(1)?  Then you use the component selector character (%).

    PRINT *, A(1)%Student_ID

will look at element 1 of A and find the component defined in the TYPE declaration as Student_ID; so, it will print:


By the same token, the following statements:

    PRINT '(1x, A5, F7.2)', A(1)%First_name, A(1)%average

will produce:

     Joe  95.50

Of course you can use the selector with READ statements, assignment statements, executions, etc.  The values can be treated like any other variable or constant.
« previous