VMS Help
FORTRAN, Statements, STRUCTURE

 *Conan The Librarian (sorry for the slow response - running on an old VAX)

  Indicates the beginning of the record structure declaration and
  defines the name of the structure.  Declaration format:

     STRUCTURE [/str/][fnlist]
                fdcl
               [fdcl]
               ...
               [fdcl]
     END STRUCTURE

     str     Identifies a structure name, which is used in
             following RECORD statements to refer to the
             structure. A structure name is enclosed in slashes.

     fnlist  Identifies field names when used in a substructure
             declaration.(Only allowed in nested structure
             declarations.)

     fdcl    (Also called the declaration body.)  Is any
             declaration or combination of declarations of
             substructures, unions, or typed data, or
             PARAMETER statements.

  Following 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
  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
  must match in type, order, and dimension.

  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 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.  The length of the structure,
  therefore, 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 following 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

  1 - Type declarations

  The syntax of a type declaration within a record structure is
  identical to that of a normal Fortran type declaration statement:
  it includes a data type (for example, INTEGER), one or more names
  of variables or arrays; and optionally, one or more data
  initialization values.

  The following rules and behavior apply to type declarations in
  record structures:

   o  %FILL can be specified in place of a field name to leave space
      in a record for purposes such as alignment.  This creates an
      unnamed field.

      %FILL can have an array declarator; for example:

         INTEGER %FILL (2,2)

      Unnamed fields cannot be initialized.  For example, the
      following statement is invalid and generates an error message:

         INTEGER*4 %FILL /1980/

   o  Initial values can be supplied in field declaration statements.
      These initial values are supplied for all records that are
      declared using this structure.  Fields not initialized will
      have undefined values when variables are declared by means of
      RECORD statements.  Unnamed fields cannot be initialized; they
      are always undefined.

   o  Field names must always be given explicit data types.  The
      IMPLICIT statement has no effect on statements within a
      structure declaration.

   o  All Fortran data types are allowed in field declarations.

   o  Any required array dimensions must be specified in the field
      declaration statements.  DIMENSION statements cannot be used to
      define field names.

   o  Adjustable or assumed sized arrays and passed-length CHARACTER
      declarations are not allowed in field declarations.

   o  Field names within the same declaration level must be unique,
      but an inner structure declaration (substructure declaration)
      can include field names used in an outer structure declaration
      without conflict.

  2 - Substructure declarations

  A field within a structure can itself be a structured item composed
  of other fields, other structures, or both.  You can declare a
  substructure in two ways:

   o  By nesting structure declarations within other structure or
      union declarations (with the limitation that you cannot refer
      to a structure inside itself at any level of nesting).

      One or more field names must be defined in the STRUCTURE
      statement for the substructure because all fields in a
      structure must be named.  In this case, the substructure is
      being used as a field within a structure or union.

      Field names within the same declaration nesting level must be
      unique, but an inner structure declaration can include field
      names used in an outer structure declaration without conflict.

      %FILL can be specified in place of a field name to leave space
      in a record for purposes such as alignment.

   o  By using a RECORD statement that specifies another previously
      defined record structure, thereby including it in the structure
      being declared.

  3 - Union declarations

  A union declaration is a multistatement declaration defining a data
  area that can be shared intermittently during program execution by
  one or more fields or groups of fields.  A union declaration must
  be within a structure declaration.  A union declaration is
  initiated by a UNION statement and terminated by an END UNION
  statement.  Enclosed within these statements are two or more map
  declarations, initiated and terminated by MAP and END MAP
  statements.  Each unique field or group of fields is defined by a
  separate map declaration.

  A union declaration takes the following form:

     UNION
          mdcl
         [mdcl]
         ...
         [mdcl]
     END UNION

     Where "mdcl" represents:

     MAP
        fdcl
       [fdcl]
       ...
       [fdcl]
     END MAP

     fdcl  Is any declaration or combination of declarations
           of substructures, unions, or type declarations.

  As with normal Fortran type declarations, data can be initialized
  in field declaration statements in union declarations.  However, if
  fields within multiple map declarations in a single union are
  initialized, the data declarations are initialized in the order in
  which the statements appear.  As a result, only the final
  initialization takes effect and all the preceding initializations
  are overwritten.

  The size of the shared area established for a union declaration is
  the size of the largest map defined for that union.  The size of a
  map is the sum of the sizes of the fields declared within it.

  As the variables or arrays declared in map fields in a union
  declaration are assigned values during program execution, the
  values are established in a record in the field shared with other
  map fields in the union.  The fields of only one of the map
  declarations are defined within a union at any given point in the
  execution of a program.  However, if you overlay one variable with
  another smaller variable, that portion of the initial variable is
  retained that is not overlaid.  Depending on the application, the
  retained portion of an overlaid variable may or may not contain
  meaningful data and can be used at a later point in the program.

  Manipulating data using union declarations is similar to the effect
  of using EQUIVALENCE statements.  The difference is that data
  entities specified within EQUIVALENCE statements are concurrently
  associated with a common storage location and the data residing
  there; with union declarations you can use one discrete storage
  location to alternately contain a variety of fields (arrays or
  variables).

  With union declarations, only one map declaration within a union
  declaration can be associated at any point in time with the storage
  location that they share.  Whenever a field within another map
  declaration in the same union declaration is referenced in your
  program, the fields in the prior map declaration become undefined
  and are succeeded by the fields in the map declaration containing
  the newly referenced field.

  In the following example, the structure WORDS_LONG is defined.
  This structure contains a union declaration defining two map
  fields.  The first map field consists of three INTEGER*2 variables
  (WORD_0, WORD_1, and WORD_2), and the second, an INTEGER*4
  variable, LONG:

     STRUCTURE /WORDS_LONG/
         UNION
             MAP
              INTEGER*2    WORD_0, WORD_1, WORD_2
             END MAP
             MAP
              INTEGER*4    LONG
             END MAP
         END UNION
     END STRUCTURE

  4 - PARAMETER Statements

  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 following PARAMETER statement declares a
  new, different symbol that 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 STATEMENTS
  PARAMETER in this Help file.
  Close     HLB-list     TLB-list     Help  

[legal] [privacy] [GNU] [policy] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.