3.2.4 Structure Constructors

A structure constructor lets you specify scalar values of a derived type. It takes the following form:

d-name (expr-list)

d-name
Is the name of the derived type.

expr-list
Is a list of expressions specifying component values. The values must agree in number and order with the components of the derived type. If necessary, values are converted (according to the rules of assignment), to agree with their corresponding components in type and kind parameters.

Rules and Behavior

A structure constructor must not appear before its derived type is defined.

If a component of the derived type is an array, the shape in the expression list must conform to the shape of the component array.

If a component of the derived type is a pointer, the value in the expression list must evaluate to an object that would be a valid target in a pointer assignment statement. (A constant is not a valid target in a pointer assignment statement.)

If all the values in a structure constructor are constant expressions, the constructor is a derived-type constant expression.

Examples

Consider the following derived-type definition:

TYPE EMPLOYEE
  INTEGER ID
  CHARACTER(LEN=40) NAME
END TYPE EMPLOYEE

This can be used to produce the following structure constructor:

EMPLOYEE(3472, "John Doe")

The following example shows a type with a component of derived type:

TYPE ITEM
  REAL COST
  CHARACTER(LEN=30) SUPPLIER
  CHARACTER(LEN=20) ITEM_NAME
END TYPE ITEM

TYPE PRODUCE
  REAL MARKUP
  TYPE(ITEM) FRUIT
END TYPE PRODUCE

In this case, you must use an embedded structure constructor to specify the values of that component; for example:

PRODUCE(.70, ITEM (.25, "Daniels", "apple"))

For More Information:

For details on pointer assignment, see Section 4.2.3.


Previous Page Next Page Table of Contents