0

我有两组全局变量,每组都存储在一个命名common块中,如下所示:

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

我希望能够对这些变量做两件不同的事情:

  • 将每组变量的值存储在一个数组中,这样最终结果将是由定义的数组integer :: a(3) = (/ x1, y1, z1 /), b(3) = (/ x2, y2, z2 /)
  • 将第二组中的值存储在第一组的对应变量中。即:x1 = x2y1 = y2z1 = z2

但是,在实际代码中,每组中的变量远不止 3 个。因此我希望能够使用循环来做到这一点。在 C/C++ 中,我可以通过递增指针轻松地做到这一点。但是,指针在 Fortran 中的工作方式不同。有没有办法做到这一点?

4

1 回答 1

0

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)
于 2020-03-10T03:02:19.243 回答