在子例程中传递一个数组数组

时间:2017-06-06 16:37:44

标签: arrays fortran

如果我在子程序中有以下代码(thanks to M. Chinoune

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array) :: raggar

allocate( raggar%vectors(2) )
allocate( raggar%vectors(1)%elements(3) )
allocate( raggar%vectors(2)%elements(4) )

raggar%vectors(1)%elements=0 
raggar%vectors(2)%elements=0 

如果我想在其他子例程中传递raggar来修改raggar的大小。我应该做点什么:

CALL MySubroutine(raggar)

CALL MySubroutine(raggar%vectors%elements)

然后,在我的子程序中,我该如何声明它?

SUBROUTINE MySubroutine(raggar)
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array), INTENT(INOUT):: raggar

我做了很多尝试,但我总是得到错误,如:

The type of the actual argument differs from the types of the dummy argument.

the shape matching rules of actual arguments and dummy argument have been violated

1 个答案:

答案 0 :(得分:1)

将类型定义放入模块中,并在程序和子程序中使用它。

module my
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array
end module

program probe
use my
type(ragged_array) :: ragarr
allocate( ragarr%vectors(2) )
allocate( ragarr%vectors(1)%elements(3) )
allocate( ragarr%vectors(2)%elements(4) )
ragarr%vectors(1)%elements=0 
ragarr%vectors(2)%elements=0 
CALL MySubroutine(ragarr)
end program

SUBROUTINE MySubroutine(rr)
use my
type(ragged_array), INTENT(INOUT):: rr
end subroutine