STRUCTURE and RECORD (The GNU Fortran Compiler)

#### 5.1.22 `STRUCTURE` and `RECORD` ¶

Record structures are a pre-Fortran-90 vendor extension to create user-defined aggregate data types. Support for record structures in GNU Fortran can be enabled with the `-fdec-structure` compile flag. If you have a choice, you should instead use Fortran 90’s “derived types”, which have a different syntax.

In many cases, record structures can easily be converted to derived types. To convert, replace `STRUCTURE /`structure-name`/` by `TYPE` type-name. Additionally, replace `RECORD /`structure-name`/` by `TYPE(`type-name`)`. Finally, in the component access, replace the period (`.`) by the percent sign (`%`).

Here is an example of code using the non portable record structure syntax:

    ! Declaring a structure named ``item'' and containing three fields:
    ! an integer ID, an description string and a floating-point price.
    STRUCTURE /item/
      INTEGER id
      CHARACTER(LEN=200) description
      REAL price
    END STRUCTURE

    ! Define two variables, an single record of type ``item''
    ! named ``pear'', and an array of items named ``store_catalog''
    RECORD /item/ pear, store_catalog(100)

    ! We can directly access the fields of both variables
    pear.id = 92316
    pear.description = "juicy D'Anjou pear"
    pear.price = 0.15
    store_catalog(7).id = 7831
    store_catalog(7).description = "milk bottle"
    store_catalog(7).price = 1.2

    ! We can also manipulate the whole structure
    store_catalog(12) = pear
    print *, store_catalog(12)

This code can easily be rewritten in the Fortran 90 syntax as following:

    ! ``STRUCTURE /name/ ... END STRUCTURE'' becomes
    ! ``TYPE name ... END TYPE''
    TYPE item
      INTEGER id
      CHARACTER(LEN=200) description
      REAL price
    END TYPE

    ! ``RECORD /name/ variable'' becomes ``TYPE(name) variable''
    TYPE(item) pear, store_catalog(100)

    ! Instead of using a dot (.) to access fields of a record, the
    ! standard syntax uses a percent sign (%)
    pear%id = 92316
    pear%description = "juicy D'Anjou pear"
    pear%price = 0.15
    store_catalog(7)%id = 7831
    store_catalog(7)%description = "milk bottle"
    store_catalog(7)%price = 1.2

    ! Assignments of a whole variable do not change
    store_catalog(12) = pear
    print *, store_catalog(12)

GNU Fortran implements structures like derived types with the following rules and exceptions:

- Structures act like derived types with the `SEQUENCE` attribute. Otherwise they may contain no specifiers.
- Structures may contain a special field with the name `%FILL`. This creates an anonymous component that cannot be accessed but occupies space just as if a component of the same type was declared in its place, useful for alignment purposes. As an example, the following structure consists of at least sixteen bytes:

      structure /padded/
        character(4) start
        character(8) %FILL
        character(4) end
      end structure

- Structures may share names with other symbols. For example, the following is invalid for derived types, but valid for structures:

      structure /header/
        ! ...
      end structure
      record /header/ header

- Structure types may be declared nested within another parent structure. The syntax is:

      structure /type-name/
          ...
          structure [/<type-name>/] <field-list>
      ...

  The type name may be omitted, in which case the structure type itself is anonymous, and other structures of the same type cannot be instantiated. The following shows some examples:

      structure /appointment/
        ! nested structure definition: app_time is an array of two 'time'
        structure /time/ app_time (2)
          integer(1) hour, minute
        end structure
        character(10) memo
      end structure

      ! The 'time' structure is still usable
      record /time/ now
      now = time(5, 30)

      ...

      structure /appointment/
        ! anonymous nested structure definition
        structure start, end
          integer(1) hour, minute
        end structure
        character(10) memo
      end structure

- Structures may contain `UNION` blocks. For more detail see the section on `UNION` and `MAP`.
- Structures support old-style initialization of components, like those described in Old-style variable initialization. For array initializers, an initializer may contain a repeat specification of the form `<literal-integer> * <constant-initializer>`. The value of the integer indicates the number of times to repeat the constant initializer when expanding the initializer list.