LF Fortran 95

Derived Type Definition

A derived type must be defined before objects of the derived type can be declared. A derived type definition specifies the name of the new derived type and the names and types of its components.







END TYPE [type-name]


derived-type-statement is a derived type statement.

private-sequence-statement is a PRIVATE statement.

or a SEQUENCE statement.

type-definition-statement is an

A type definition statement in a derived type definition can have only the POINTER and DIMENSION attributes. It cannot be a function. It can be given a default initialization value, in which case the component acquires the SAVE attribute. A component array must be a deferred-shape array if the POINTER attribute is present, otherwise it must have an explicit shape.

type coordinates real :: latitude, longitude end type coordinates type place character(len=20) :: name type(coordinates) :: location end type place type link integer :: j type (link), pointer :: next end type link

In the example, type coordinates is a derived type with two REAL components: latitude and longitude. Type place has two components: a CHARACTER of length twenty, name, and a structure of type coordinates named location. Type link has two components: an INTEGER, j, and a structure of type link, named next, that is a pointer to the same derived type. A component structure can be of the same type as the derived type itself only if it has the POINTER attribute. In this way, linked lists, trees, and graphs can be formed.

There are two ways to use a derived type in more than one program unit. The preferred way is to define the derived type in a module (see See Module Program Units) and use the module wherever the derived type is needed. Another method, avoiding modules, is to use a SEQUENCE statement in the derived type definition, and to define the derived type in exactly the same way in each program unit the type is used. This could be done using an include file. Components of a derived type can be made inaccessible to other program units by using a PRIVATE statement before any component definition statements.