【发布时间】:2020-12-05 10:14:02
【问题描述】:
我想在子程序中分配一个数组,然后在主程序中使用这个数组并将它传递给其他子程序。过去(F77?)传递可以在一个公共块中完成,但现在更受欢迎的过程似乎是使用一个模块。当我尝试这个时,如代码示例中所示,编译器会告诉我
Rank mismatch in argument ‘f’ at (1) (scalar and rank-1)
显然,主程序认为 'f' 是一个标量:但是,我读到这段代码的意思是,我在子程序和主程序中都将它声明为一维数组。我错过了什么?
我已经尝试过变体,例如将变量声明为模块的一部分,但我想不出任何办法使编译没有错误(而且有些产生了更多错误 ;-( )。任何见解都非常感谢。
module subs
contains
subroutine makef(f)
end subroutine makef
end module subs
c-----------------------------------------------------------------------
program work
use subs
implicit none
real, allocatable :: f(:)
call makef(f)
write (*,*) f
stop
end
c---------------------------------------------------------------------
subroutine makef(f)
implicit none
real, allocatable, intent(out) :: f(:)
integer :: i
integer :: is
is=10
allocate(f(-is:is))
do i=-is,is
f(i)=i
end do
return
end subroutine makef
【问题讨论】:
-
对所有 Fortran 问题使用标签 fortran。