## B.12.1 Structure Declarations

A structure declaration 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, enclosed by slashes.

  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.

  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 (Section B.12.1.1)

    These are ordinary Fortran data type declarations.

  - Substructure declarations (Section B.12.1.2)

    A field within a structure can be a substructure composed of atomic fields, other substructures, or a combination of both.

  - Union declarations (Section B.12.1.3)

    A union declaration is composed of one or more mapped field declarations.

  - PARAMETER statements

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

    Type declarations for PARAMETER 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)

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.

By default, fields are aligned on natural boundaries; misaligned fields are padded as necessary. To avoid padding of records, you should lay out structures so that all fields are naturally aligned.

To pack fields on arbitrary byte boundaries, you must specify a compiler option. You can also specify alignment for fields by using the cDEC$ OPTIONS or cDEC$ PACK general directive.

A field name must not be the same as any intrinsic or user-defined operator (for example, EQ cannot be used as a field name).

Examples

In the following 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 /DATE/
       INTEGER*1 DAY, MONTH
       INTEGER*2 YEAR
     END STRUCTURE

     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 B-1 shows the memory mapping of any record or record array element with the structure APPOINTMENT.

Figure B-1 Memory Map of Structure APPOINTMENT

For More Information:

- On compiler options, see your user manual or programmer's guide.
- On the cDEC$ OPTIONS directive, see Section 15.1.14.

Previous Page Next Page Table of Contents