Lahey/GNU Fortran

Next: , Up: Extensions not implemented in GNU Fortran


6.2.1 STRUCTURE and RECORD

Structures are user-defined aggregate data types; this functionality was standardized in Fortran 90 with an different syntax, under the name of “derived types”. Here is an example of code using the non portable 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)