# 4.17 Structure Declarations

A structure declaration defines one or more fields within a Fortran record. It defines the field names, types of data within fields, and order and alignment of fields within a record. Fields and structures can be initialized, but records cannot be initialized.

A structure declaration takes the following form:

    STRUCTURE [/structure-name/][field-namelist]

       field-declaration

       [field-declaration]

        . . .

       [field-declaration]

    END STRUCTURE

structure-name
  Is the name used to identify a structure. A structure name is enclosed by slashes. If the slashes are present, a name must be specified between them.

  Subsequent RECORD statements use the structure name to refer to the structure. A structure name must be unique among structure names, but structures can share names with variables (scalar or array), record fields, PARAMETER constants, and common blocks. So, it is possible to have a variable named X, a structure named X, one or more fields named X, a PARAMETER constant named X, and a common block named X.

  Structure declarations can be nested (contain one or more other structure declarations). A structure name is required for the structured declaration at the outermost level of nesting, and is optional for the other declarations nested in it. However, if you wish to reference a nested structure in a RECORD statement in your program, it must have a name.

  Structure, field, and record names are all local to the defining program unit. When records are passed as arguments, the fields in the defining structures within the calling and called subprograms must match in type, order, and dimension.

field-namelist
  Is a list of fields having the structure of the associated structure declaration. A field namelist is allowed only in nested structure declarations.
field-declaration
  Also called the declaration body. A field-declaration consists of any combination of the following:

  - Type declarations: These are ordinary Fortran data type declarations. Fields can have any data type and can be dimensioned in the normal way. For more information on type declarations in structure declarations, see Section 4.17.1.

  - Substructure declarations: A field within a structure can be a substructure composed of atomic fields, other substructures, or a combination of both. For more information on substructure declarations in structure declarations, see Section 4.17.2.

  - Union declarations: A union declaration is composed of one or more mapped field declarations. The mapped fields logically share a common location within a structure. For more information on union declarations in structure declarations, see Section 4.17.3.

  - PARAMETER statements: PARAMETER statements can appear in a structure declaration, but cannot be given a data type within the declaration block. Consider the following:

        STRUCTURE /ABC/

          INTEGER*4 P

          PARAMETER (P=4)

          REAL*4 F

        END STRUCTURE

          REAL*4 A(P)

    In this example, the INTEGER*4 statement does not provide the data type for PARAMETER constant P, but instead declares a record field P in structure ABC. The subsequent PARAMETER statement declares a new, different symbol which is given the implicit data type for identifiers beginning with the letter P.

    Type declarations for PARAMETER symbolic names must precede the PARAMETER statement and be outside of a STRUCTURE declaration, as follows:

          INTEGER*4 P

        STRUCTURE /ABC/

          PARAMETER (P=4)

          REAL*4 F

        END STRUCTURE

          REAL*4 A(P)

    For more information on PARAMETER statements, see Section 4.12.

Rules and Behavior

Unlike type declaration statements, structure declarations do not create variables. Structured variables (records) are created when you use a RECORD statement containing the name of a previously declared structure. The RECORD statement can be considered as a kind of type declaration statement. The difference is that aggregate items, not single items, are being defined.

Within a structure declaration, the ordering of both the statements and the field names within the statements is important because this ordering determines the order of the fields in records.

In a structure declaration, each field offset is the sum of the lengths of the previous fields, so the length of the structure is the sum of the lengths of its fields. The structure is packed; you must explicitly provide any alignment that is needed by including, for example, unnamed fields of the appropriate length.

Examples

In the first example, the declaration defines a structure named DATE. This structure contains three scalar fields: DAY (LOGICAL*1), MONTH (LOGICAL*1), and YEAR (INTEGER*2).

    STRUCTURE /DATE/

      LOGICAL*1 DAY, MONTH

      INTEGER*2 YEAR

    END STRUCTURE

Figure 4-5 shows the memory mapping of any record or record array element with the structure DATE:

### Figure 4-5 Memory Map of Structure DATE

In the second example, the declaration defines a structure named APPOINTMENT. APPOINTMENT contains the structure DATE (field APP_DATE) as a substructure. It also contains a substructure named TIME (field APP_TIME, an array), a CHARACTER*20 array named APP_MEMO, and a LOGICAL*1 field named APP_FLAG.

    STRUCTURE /APPOINTMENT/

      RECORD /DATE/     APP_DATE

      STRUCTURE /TIME/  APP_TIME (2)

        INTEGER*1       HOUR, MINUTE

      END STRUCTURE

      CHARACTER*20      APP_MEMO (4)

      LOGICAL*1         APP_FLAG

    END STRUCTURE

The length of any instance of structure APPOINTMENT is 89 bytes.

Figure 4-6 shows the memory mapping of any record or record array element with the structure APPOINTMENT.

### Figure 4-6 Memory Map of Structure APPOINTMENT

The following sections describe type declarations, substructure declarations, and union declarations.

For More Information:

- On type declaration statements, in general, see Section 4.5.

- On the RECORD statement, see Section 4.15.

- On nested structure declarations, see Section 4.17.2.

Previous Page Next Page Table of Contents