有没有办法制作切片指针数组?

时间:2014-05-22 09:58:49

标签: pointers multidimensional-array fortran

我想创建一个二维指针数组,链接到另一个一维数组:

   module class_GridUnit;use iso_fortran_env
   implicit none

   type,public :: CGridUnit
     integer(int8)::index
   endtype CGridUnit

   type,public :: CGridLink
     type(CGridUnit),pointer::L
   endtype CGridLink

   type(CGridUnit),target,allocatable,dimension(:)::gDATA
   type(CGridLink),allocatable,dimension(:,:)::gLink

   endmodule class_GridUnit

   program tt
   use class_GridUnit
   integer::ix1=5,iy1=5,i1=20
   integer(int8),dimension(2,2)::c=0

   allocate ( gLink(ix1,iy1) )
   forall(ix=1:ix1,iy=1:iy1)
          gLink(ix,iy)%L=>null()
   endforall
   allocate ( gDATA(20) )

   i0=0; do ix=1,ix1; do iy=1,iy1 ; i0=i0+1
     if(i0<=20)then
       gLink(ix,iy)%L=>gDATA(i0)
       gLink(ix,iy)%L%index=i0
     endif
   enddo; enddo

   forall(ix=1:2,iy=1:2) c(ix,iy)=gLink(ix,iy)%L%index
   print *, c

   end

forall工作得很好,但是当我想要切片时,我得到了:

   c(1:2,1:2)=gLink(1:2,1:2)%L%index
       1
   Error: Component to the right of a part reference with nonzero rank must not have the POINTER attribute at (1)

所以,我的问题 - 是否有某种方法可以做到这一点?

1 个答案:

答案 0 :(得分:4)

如果派生类型的任何组件为pointerallocatable,则无法在Fortran中使用此简写表示法。

标准禁止它,因为表达式gLink(1:2,1:2)%L%index产生的数组不仅是非连续的,而且甚至没有恒定的步幅。使用指针数组,指针的目标可以随机放置在内存中。因此,您必须使用循环或类似的构造,例如forall,它可以正常工作,如您所写:

forall(ix=1:2,iy=1:2) c(ix,iy)=gLink(ix,iy)%L%index
相关问题