Ste*_*ini 5 arrays fortran fortran90
我有以下程序
module test
contains
subroutine foo()
integer, allocatable :: a(:)
allocate(a(-5:5))
call bar(a)
print *, a
end subroutine
subroutine bar(a)
integer, intent(out) :: a(:)
a = 0
a(-4) = 3 ! here
a(2) = 3
end subroutine
end module
program x
use test
call foo()
end program
Run Code Online (Sandbox Code Playgroud)
在标有"here"的行中,我做错了.事实是,当我收到数组时a(在从-5到+5分配的调用者中),被调用者使用常规编号(1到n),这意味着分配-4我正在进行边界分配.如何指示编译器在bar例程中,a数组的编号必须与调用者中的编号相同?
您在子例程中使用的虚拟参数类型(尺寸用冒号指定)称为“假定形状”。这个名字就是线索——Fortran 只传递形状,而不传递下限和上限。下限被假定为 1,除非您按照 kemiisto 的答案中所示覆盖它。如果下限不固定,您可以传递一个参数作为下限。
稍后添加:如果编译时不知道较低维度的代码示例:
subroutine example (low, array)
integer, intent (in) :: low
real, dimension (low:), intent (out) :: array
Run Code Online (Sandbox Code Playgroud)