How to pass allocatable arrays to subroutines in Fortran

前端 未结 3 1901
日久生厌
日久生厌 2020-11-27 06:38

The following code is returning a Segmentation Fault because the allocatable array I am trying to pass is not being properly recognized (size returns 1, when it should be 3)

3条回答
  •  日久生厌
    2020-11-27 07:22

    This is a simple example that uses allocatable dummy arguments with a module.

    module arrayMod   
      real,dimension(:,:),allocatable :: theArray    
    end module arrayMod
    
    program test
       use arrayMod
       implicit none
    
       interface
          subroutine arraySub
          end subroutine arraySub
       end interface
    
       write(*,*) allocated(theArray)
       call arraySub
       write(*,*) allocated(theArray) 
    end program test
    
    subroutine arraySub
       use arrayMod
    
       write(*,*) 'Inside arraySub()'
       allocate(theArray(3,2))
    end subroutine arraySub
    

提交回复
热议问题