How to assign to/get value of variable at next memory address in Fortran

328 Views Asked by At

I have two sets of global variables, which are each stored in a named common block like this:

integer :: x1, y1, z1, x2, y2, z2
common/vars/ x1, y1, z1
common/vars/ x2, y2, z2

There are two different things I want to be able to do with these variables:

  • Store the values of each set of variables in an array, such that the end result would be the arrays defined by integer :: a(3) = (/ x1, y1, z1 /), b(3) = (/ x2, y2, z2 /)
  • Store the values in the second set in their counterpart variables of the first set. That is: x1 = x2, y1 = y2, and z1 = z2.

However, in the actual code there are a lot more than 3 variables in each set. Therefore I want to be able to do this using loops. In C/C++ I would be able to do this easily by incrementing pointers. However, pointers do not work like that in Fortran. Is there any way to accomplish this?

1

There are 1 best solutions below

0
Mark Diaz On

Don't know what your Fortran compiler supports, but here's a few ideas (hacks) using DEC Fortran-77.

  1. Similar to EQUIVALENCE, then change x1 refererences to xyzzy.x1, etc. You can reference the arrays a() and b().
STRUCTURE /MY_STRUCT/
 UNION
  MAP
    INTEGER X1, Y1, Z1, X2, Y2, Z2
  ENDMAP
  MAP
    INTEGER A(3), B(3)
  ENDMAP
 ENDUNION
ENDSTRUCTURE
COMMON /VARS/ XYZZY
RECORD /MY_STRUCT/ XYZZY
  1. Fool a subroutine to think you have passed an array (using your COMMON). May need to be compiled as a separate source code file to avoid compiler warnings.
 CALL MY_SUB( X1 )
 [...]

 SUBROUTINE MY_SUB( A )
 INTEGER A(3)