next up previous contents
Next: Initialisation of Variables and Up: Introduction to FORTRAN Previous: COMMON statements

   
EQUIVALENCE statements

These statements allow the user to supply alternative names to variables and arrays. These may be local or in common blocks but must not be arguments passed to the routine containing the statement. The following statements illustrate their use:-

 
    COMMON/SUMS/ NSUM,SUMX,SUMY,SUMXY,SUMYY,SUMXX
    DIMENSION SUMS(5)
    EQUIVALENCE (SUMS(1),SUMX)
  
    DIMENSION Q(1000),IQ(2)
    EQUIVALENCE (Q,IQ)
 
    DIMENSION ARRAY(10,5)
    EQUIVALENCE (V1,V2,V3,ARRAY(7,3)), (ARRAY(2,2),V4)

The first 3 statements equivalence the array SUMS to the second word of the COMMON /SUMS/ i.e. SUMS(1) is the same location in memory as SUMX. Because the elements in an array, and the variables and arrays in commons, are contiguous this means that SUMS(2) is the same as SUMY, SUMS(3) is SUMXY etc. The next two statements define alternative names for an array of 1000 words. This would allow reals and integers to be held in the same array. Note that as IQ is the same as Q, it is not necessary to declare its full length, IQ(1000) is still the same as Q(1000). The last two statements define ARRAY and supplies 3 alternative names for the element ARRAY(7,3) namely V1,V2, and V3, and the alternative name V4 for ARRAY(2,2) Although EQUIVALENCEs may associate different data types the one exception (as always) is character which can only be equivalenced to itself. As stated above, you cannot use EQUIVALENCEs with arguments passed to a routine e.g:-

    SUBROUTINE INPUT(A)
    EQUIVALENCE (A,B)
is illegal. An EQUIVALENCE must be consistant with organisations of arrays in memory e.g.:-

 
    DIMENSION A(5),B(5)
    EQUIVALENCE (A(1),B(1)),(A(2),B(3)

is illegal because if A(1) is the same as B(1) then A(2) must be B(2) and not B(3). Also a common block cannot be implicibly extended before its start point e.g.:

 
    COMMON/COMM/ A
    DIMENSION B(5)
    EQUIVALENCE (A,B(3))

is illegal for B(1) would start before the beginning of /COMM/. Extending the length of a common block is alright so:-

EQUIVALENCE (A,B(1))
would be fine.


next up previous contents
Next: Initialisation of Variables and Up: Introduction to FORTRAN Previous: COMMON statements
n west (APC)
2000-03-08