I have the following structure:
PROGRAM myPro USE mMod1 IMPLICIT NONE REAL t INTEGER i CALL proc1 ! do something... ! I can access here dt defined in mMod1 ! but not myArr1 END PROGRAM myPro
MODULE mMod1 IMPLICIT NONE REAL dt REAL, ALLOCATABLE :: myArr1(:,:) CONTAINS SUBROUTINE proc1 USE mMod2 CHARACTER(LEN=256) fn CALL proc2(fn,myArr1) ! I can access here both dt and myArr1 END SUBROUTINE proc1 END MODULE mMod1
MODULE mMod2 IMPLICIT NONE CONTAINS SUBROUTINE proc2(fn,myArr1) CHARACTER(LEN=256), INTENT(IN) :: fn REAL, ALLOCATABLE :: myArr1(:,:) INTEGER n INTEGER, PARAMETER :: dim = 3 ! read some input file and determine n ALLOCATE (myArr1(dim,n)) ! fill in the array by reading the rest of the file END SUBROUTINE proc2 END MODULE mMod2
The question is, the definition of both dt and myArr is the same except that myArr is an allocatable array allocated in a separate module. But I cannot access myArr1 in the main program while I can access dt in the main program. Why? And how to make changes so that I can access it there.
The whole purpose is: I want to read an input file in a separate subroutine into an array (myArr1) and have this array available globaly. This is why I declare it in mMod1. The dimensions of the array are written at the top of the file so I do not know them until I have started reading the file. So the allocation of the myArr1 has to happen inside the separate procedure, I would include it as a separate procedure but it does not work as probably INTERFACE is necessary, so I decided to wrap the procedure in a separate module.