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
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.
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.
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.
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:
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.
The following sections describe type declarations, substructure declarations, and union declarations.