# STRUCTURE and END STRUCTURE

Statement: Defines the field names, types of data within fields, and order and alignment of fields within a record structure. Fields and structures can be initialized, but records cannot be initialized.

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 These are ordinary Fortran data type declarations. Substructure declarations A field within a structure can be a substructure composed of atomic fields, other substructures, or a combination of both. Union declarations 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)

The Standard Fortran derived type replaces STRUCTURE and RECORD constructs, and should be used in writing new code. See Derived Data Types.

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 OPTIONS or 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).

## Example

An item can be a RECORD statement that references a previously defined structure type:

     STRUCTURE /full_address/
       RECORD  /full_name/ personsname
       RECORD  /address/   ship_to
       INTEGER*1           age
       INTEGER*4           phone
     END STRUCTURE

You can specify a particular item by listing the sequence of items required to reach it, separated by a period (.). Suppose you declare a structure variable, `shippingaddress`, using the `full_address`structure defined in the previous example:

     RECORD /full_address/ shippingaddress

In this case, the `age`item would then be specified by `shippingaddress.age`, the first name of the receiver by `shippingaddress.personsname.first_name`, and so on.

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.

The following figure shows the memory mapping of any record or record array element with the structure `APPOINTMENT`.

Memory Map of Structure APPOINTMENT
Parent topic: S

## See Also

TYPE
MAP...END MAP
RECORD
UNION...END UNION
PACK Directive
OPTIONS Directive
Data Types, Constants, and Variables
Record Structures