【发布时间】:2017-08-04 15:03:18
【问题描述】:
我希望能够重置数组中不在连续内存块中的元素。我想为此使用一个指针数组而不是指针数组,因为我对指针数组的理解是它们必须指向一个连贯的内存块(例如指针(1:10,1:10)=>目标(1 :100))
我的简单测试程序如下:
program test
implicit none
integer, target :: arr(4,4)
type ptr
integer, pointer :: p
end type ptr
type(ptr), dimension(2) :: idx
arr=0
idx(1)%p=>arr(2,2)
idx(2)%p=>arr(4,2)
idx(1)%p=5 ! this is okay
idx(2)%p=5 ! this is okay
idx(1:2)%p=5 ! this gives an error
print *,arr
end program test
前两个语句 idx(n)%p=5 没问题,但我希望能够使用跨越方法 idx(1:n)%p=5 在一个语句中设置数组的一个块,但是当我这样做我得到以下编译错误:
Error: Component to the right of a part reference with nonzero rank must not have the POINTER attribute at (1)
我可以以某种方式使用指针设置一大块数组条目的值吗?也许实际上可以使用指针数组而不是指针数组...
我认为这与Fortran: using a vector to index a multidimensional array有关
但我在这里看不到如何使用该答案。
【问题讨论】: