我想将可分配数组的一部分传递给子例程。源阵列由三个指标组成。子例程期待一个包含两个索引的数组。假设我想对源数组的第五个索引进行操作。我做了这样的事,但最后一句被抓住了:
module lots_of_stuff
...
contains
subroutine process_points(points)
integer, allocatable, intent(inout) :: points(:,:)
! do stuff
end subroutine
end module
program foo
use lots_of_stuff
integer, allocatable :: inpoints(:,:,:)
integer :: lim1, lim2, lim3
! figure out how big inpoints should be (i.e. set lim1, lim2, lim3) then ...
allocate(inpoints(lim1,lim2,lim3)
! populate inpoints then ...
call process_points(????)假设我想要处理第五节中的元素。如果我试着
call process_points(inpoints(:,:,5))gnu fortran告诉我
Error: Actual argument for ‘badpts’ must be ALLOCATABLE at (1)如果我试着
call process_points(inpoints(1,1,5))gnu fortran告诉我
Error: Rank mismatch in argument ‘badpts’ at (1) (rank-2 and scalar)我想我可以将inpoints(:,:,5)复制到一个包含两个索引的数组中,并将其发送到process_points,但这似乎不太优雅。有办法做我想做的事吗?
发布于 2018-12-03 19:55:45
除非您想要释放或分配虚拟参数,否则不应该使process_points(:)内部的虚拟参数可分配。或者,如果希望通过调用保留下限,但实际参数必须是可分配的。
子数组是不可分配的,即使它是可分配数组的子数组。因此,绝不能将子数组传递给需要可分配参数的子例程。
因此,我只需删除子例程中的allocatable属性。
https://stackoverflow.com/questions/53600904
复制相似问题