|Fortran Language Reference Manual, Volume 1 - S-3692-51|
|Prev Section||Chapter 6. Using Data||Next Section|
A structure is an aggregate of components of intrinsic, derived types, or type aliases. It is itself an object of derived type. The types and attributes of the components are specified in the type definition; they can be scalars or arrays. Each structure has at least one component. There can be arrays of structures. In example 2 (see Section 6.2), FIRECHIEF is a structure; FIREMEN is an array of structures of type PERSON.
A component of a structure is referenced by placing the name of the component after the name of the parent structure, separated by a percent sign (%). For example, FIRECHIEF % NAME references the character string component of the variable FIRECHIEF of type PERSON.
A structure component is a data reference and is defined as follows:
For a data reference to be considered a structure component reference, there must be more than one part reference and the rightmost part reference must be a part name. If the rightmost component is of the following form, the reference is considered to be an array section or array element (the simplest form of a section subscript list is a subscript list):
part_name (section_subscript_list )
The rules for forming a section_subscript_list and a subscript are provided in Section 6.4.
In a data reference, each part name except the rightmost name must be of derived type or of type alias that describes a derived type.
In a data reference, each part name except the left-most must be the name of a component of the derived-type definition or type alias definition of the type of the preceding part name.
In a part reference containing a section subscript list, the number of section subscripts must equal the rank of the part name.
It is possible to create a structure with more than one array part, but in a data reference to the structure, there must not be more than one part reference with nonzero rank.
In a data reference, a part name to the right of a part reference with nonzero rank must not have the POINTER attribute. It is possible to declare an array of structures that have a pointer as a component, but it is not possible to reference such an object as an array.
The rank of a part reference consisting of just a part name is the rank of the part name. The rank of a part reference of the following form is the number of subscript triplets and vector subscripts in the list:
part_name (section_subscript_list )
The rank is less than the rank of the part name if any of the section subscripts are subscripts other than subscript triplets or vector subscripts. The shape of a data reference is the shape of the part reference with nonzero rank, if any; otherwise, the data reference is a scalar and has rank zero.
The parent structure in a data reference is the data object specified by the left-most part name. If the parent object has the INTENT, TARGET, or PARAMETER attribute, the structure component has the attribute. The type and type parameters of a structure component are those of the rightmost part name. A structure component is a pointer only if the rightmost part name has the POINTER attribute. Typically, an object cannot have both the TARGET and POINTER attributes. However, a structure that has the TARGET attribute can contain components that have the POINTER attribute.
Example 1: Assume the following type definition and structure declarations:
TYPE PERSON INTEGER AGE CHARACTER(LEN = 40) NAME END TYPE PERSON . . . TYPE(PERSON) FIRECHIEF, FIREMEN(50)
In this example, structure components are as follows:
FIRECHIEF%AGE ! scalar component of scalar parent FIREMEN(J)%NAME ! component of array element parent FIREMEN(1:N)%AGE ! component of array section parent
Example 2: If a derived-type definition contains a component that is of derived type, then a reference to an ultimate component can contain more than two part references as do the references in the first two PRINT statements in the following example:
TYPE REPAIR_BILL REAL PARTS REAL LABOR END TYPE REPAIR_BILL . . . TYPE VEHICLE CHARACTER(LEN = 40) OWNER INTEGER MILEAGE TYPE(REPAIR_BILL) COST END TYPE VEHICLE . . . TYPE(VEHICLE) BLACK_FORD, RED_FERRARI . . . PRINT *, BLACK_FORD%COST%PARTS PRINT *, RED_FERRARI%COST%LABOR PRINT *, RED_FERRARI%OWNER