A reference to a component of a derived-type structure takes the following form:
Each subscript must be a scalar integer (or other numeric) expression with a value that is within the bounds of its dimension.
The number of subscripts in any s-list must equal the rank of the immediately preceding parent or component.
Rules and Behavior
Each parent or component (except the rightmost) must be of derived type.
The parent or one of the components can have nonzero rank (be an array). Any component to the right of a parent or component of nonzero rank must not have the POINTER attribute.
The rank of the structure component is the rank of the part (parent or component) with nonzero rank (if any); otherwise, the rank is zero. The type and type parameters (if any) of a structure component are those of the rightmost part name.
The structure component must not be referenced or defined before the declaration of the parent object.
If the parent object has the INTENT, TARGET, or PARAMETER attribute, the structure component also has the attribute.
The following example shows a derived-type definition with two components:
TYPE EMPLOYEE INTEGER ID CHARACTER(LEN=40) NAME END TYPE EMPLOYEE
The following shows how to declare CONTRACT to be of type EMPLOYEE:
TYPE(EMPLOYEE) :: CONTRACT
Note that both examples started with the keyword TYPE. The first (initial) statement of a derived-type definition is called a derived-type statement, while the statement that declares a derived-type object is called a TYPE statement.
The following example shows how to reference component ID of parent structure CONTRACT:
The following example shows a derived type with a component that is a previously defined type:
TYPE DOT REAL X, Y END TYPE DOT .... TYPE SCREEN TYPE(DOT) C, D END TYPE SCREEN
The following declares a variable of type SCREEN:
Variable M has components M%C and M%D (both of type DOT); M%C has components M%C%X and M%C%Y of type REAL.
The following example shows a derived type with a component that is an array:
TYPE CAR_INFO INTEGER YEAR CHARACTER(LEN=15), DIMENSION(10) :: MAKER CHARACTER(LEN=10) MODEL, BODY_TYPE*8 REAL PRICE END TYPE ... TYPE(CAR_INFO) MY_CAR
Note that MODEL has a character length of 10, but BODY_TYPE has a character length of 8. You can assign a value to a component of a structure; for example:
MY_CAR%YEAR = 1985
The following shows an array structure component:
In the preceding example, if a subscript list (or substring) was appended to MAKER, the reference would not be to an array structure component, but to an array element or section.
Consider the following:
In this case, the component is substring 4 to 10 of the second element of array MAKER.
Consider the following:
TYPE CHARGE INTEGER PARTS(40) REAL LABOR REAL MILEAGE END TYPE CHARGE TYPE(CHARGE) MONTH TYPE(CHARGE) YEAR(12)
Some valid array references for this type follow:
MONTH%PARTS(I) ! An array element MONTH%PARTS(I:K) ! An array section YEAR(I)%PARTS ! An array structure component (a whole array) YEAR(J)%PARTS(I) ! An array element YEAR(J)%PARTS(I:K) ! An array section YEAR(J:K)%PARTS(I) ! An array section YEAR%PARTS(I) ! An array section
The following example shows a derived type with a pointer component that is of the type being defined:
TYPE NUMBER INTEGER NUM TYPE(NUMBER), POINTER :: START_NUM => NULL( ) TYPE(NUMBER), POINTER :: NEXT_NUM => NULL( ) END TYPE
A type such as this can be used to construct linked lists of objects of type NUMBER. Note that the pointers are given the default initialization status of disassociated.
The following example shows a private type:
TYPE, PRIVATE :: SYMBOL LOGICAL TEST CHARACTER(LEN=50) EXPLANATION END TYPE SYMBOL
This type is private to the module. The module can be used by another scoping unit, but type SYMBOL is not available.
For More Information