EQUIVALENCE - Fortran

The EQUIVALENCE statement allows two or more entities in a program unit to share storage units, thus associating those entities. This statement allows the same information to be referenced by different names in the same program unit.

Syntax
EQUIVALENCE (nlist) [,(nlist)] ... where nlist is a list of variable names, array element names, array names, and character substring names.

Method of Operation
The storage sequences of the entities in the list must have the same first storage unit. This requirement associates the entities in the list or other elements as well. The EQUIVALENCE statement only associates storage units and does not cause type conversion or imply mathematical equivalence. Thus, if a variable and an array are equivalenced, the variable does not assume array properties and vice versa.

Character entities can be associated by equivalence only with other character entities. Specify the character entities, character variables, character array names, character array element names, or character substring names. Association is made between the first storage units occupied by the entities appearing in the equivalence list of an EQUIVALENCE statement. This statement can associate entities of other character elements as well. The lengths of the equivalenced character entities are not required to be equal.

Variables and arrays can be associated with entities in common storage to lengthen the common block. However, association through the use of the EQUIVALENCE statement must not cause common storage to be lengthened by adding storage units before the first storage unit in the common block.

Rules for Use

  • Each subscript expression or substring expression in an equivalence list must be an integer constant expression.
  • If an array element name is specified in an EQUIVALENCE statement, the number of subscript expressions must be the same as the number of dimensions declared for that array.
  • An array name without a subscript is treated as an array element name that identifies the first element of the array.
  • Multidimensional array elements can be referred to in an EQUIVALENCE statement with only one subscript. The compiler considers the array to be one-dimensional according to the array element ordering of Fortran. Consider the following example:
DIMENSION a(2,3), b(4:5,2:4)

The following shows a valid EQUIVALENCE statement using the arrays a and b:

EQUIVALENCE (a(1,1), b(4,2))

The following example achieves the same effect:

EQUIVALENCE (a(1), b(4))

The lower-bound values in the array declaration are always assumed for missing subscripts (in the above example, 1 through 3 for array a and 2 through 4 for array b).

Restrictions

  • Names of dummy arguments of an external procedure in a subprogram cannot appear in an equivalence list.
  • A variable name that is also a function name cannot appear in the list.
  • A storage unit can appear in no more than one EQUIVALENCE storage sequence.
  • An EQUIVALENCE statement cannot specify non-consecutive storage positions for consecutive storage units.
  • An EQUIVALENCE statement cannot associate a storage unit in one common block with any storage unit in a different common block.

Example
The two statements below are represented in storage as shown in Figure below.

DIMENSION M(3,2),P(6)
EQUIVALENCE (M(2,1),P(1))

Figure: Storage Representation of an EQUIVALENCE Statement

Storage Representation of an EQUIVALENCE Statement
Example
The two statements below cause the logical representation in storage shown in Figure below.

CHARACTER ABT*6, BYT(2)*4, CDT*3
EQUIVALENCE (ABT, BYT(1)),(CDT, BYT(2))

Figure: Logical Representation of an EQUIVALENCE Statement

Logical Representation of an EQUIVALENCE Statement
Example
The following statements are invalid because they specify non-consecutive storage positions for consecutive storage units.

REAL A(2)
DOUBLE PRECISION S(2)
EQUIVALENCE (A(1), S(1)), (A(2), S(2))

Note that a double-precision variable occupies two consecutive numeric storage units in a storage sequence.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Fortran Topics