请问Fortran里动态数组可以被设置成全局变量吗?如果可以要怎么设置呢?

2025-02-26 06:21:44
推荐回答(2个)
回答1:

fortran 没有全局变量。


共享数据的方式有:

  1. 虚参传递。可使用动态数组

  2. common 公共区。不可使用动态数组,老语法,不推荐。

  3. module 模块。可使用动态数组,强力推荐

回答2:

例:

program main
    use subs
    implicit none
    
    real(8) :: a(5)
    call test1
    call test2(a)
    print *, a
    
end program

module subs
    contains
    subroutine test1()
        use sharemod
        implicit none
        
        allocate(share(5))
        share = (/1., 2., 3., 5., 6./)
    end subroutine
     
    subroutine test2(output)
        use sharemod
        implicit none
        real(8), intent(out) :: output(5)
        output = share
    end subroutine test2
end module subs

module sharemod
    implicit none 
    save
    real(8), allocatable, dimension(:) :: share
    
end module sharemod