Share allocatable Arrays

后端 未结 3 841
离开以前
离开以前 2021-01-23 01:06

I have some allocatable arrays which I need to share between some subroutines. I usually would just pass them as arguments or maybe write everything in a Module, but I\'m afraid

3条回答
  •  旧时难觅i
    2021-01-23 01:55

    I do not understand why writing a MODULE would not work, but have you considered CONTAINS? Everything above the CONTAINS declaration is visible to the subroutines below the CONTAINS:

    PROGRAM call_both
       INTEGER,DIMENSION(2) :: a, b
       a = 1
       b = 2
       PRINT *,"main sees", a, b
       CALL subA
       CALL subB
     CONTAINS
       SUBROUTINE subA
          PRINT *,"subA sees",a,b
       END SUBROUTINE subA
    
       SUBROUTINE subB
          PRINT *,"subB sees",a,b
       END SUBROUTINE subB
    END PROGRAM call_both
    

    The output would be

    main sees           1           1           2           2
    subA sees           1           1           2           2
    subB sees           1           1           2           2
    

    This works with ALLOCATABLE arrays as well.

提交回复
热议问题