Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Structure Components

A reference to a component of a derived-type structure takes the following form:

parent [(s-list)] [image-selector] [%component [(s-list)] [image-selector]] ... %component [(s-list)] [image-selector]

parent

Is the name of a scalar or array of derived type. The percent sign (%) is called a component selector.

s-list

Is a list of one or more subscripts. If the list contains subscript triplets or vector subscripts, the reference is to an array section.

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.

image-selector

Is an image selector if the parent or component is a coarray. For more information, see Image Selectors.

component

Is the name of a component of the immediately preceding parent or component.

Description

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.

If the rightmost component is of type C_PTR or C_FUNPTR from the intrinsic module ISO_C_BINDING, or of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV, each parent or component must not have an image-selector.

Examples

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:

  CONTRACT%ID

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:

  TYPE(SCREEN) M

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 = 2020

The following shows an array structure component:

  MY_CAR%MAKER

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:

  MY_CAR%MAKER(2) (4:10)

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.

The following example shows how to dereference an array of structures with a coarray structure component that is a real array:

  TYPE REAL_ARRAY
    REAL,ALLOCATABLE    :: RA(:)
  END TYPE

  TYPE CO_ARRAY
    TYPE(REAL_ARRAY)    :: CA[*]
  END TYPE

  TYPE STRUCT
    TYPE(CO_ARRAY)      :: COMP
  END TYPE

  TYPE(STRUCT)          :: ARRAY(10, 10)
  
  ALLOCATE(ARRAY(5,10)%COMP%CA%RA(10))
  ARRAY(5,10)%COMP%CA[5]%RA(:) = 0

See Also