2014-05-22 58 views
1

我要創建哪些鏈接到另一個一維數組指針的二維數組:有沒有辦法讓指針數組切片?

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) 

所以,我的問題 - 有沒有辦法做到這一點?

回答

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 
相關問題