如果需要复杂数组,则传递真实数组
Pass a real array if a complex array is expected
我有一个 Fortran 子例程,它需要一个像
这样的复杂数组
subroutine foo(cnumbers, n)
integer :: n
complex :: cnumbers(n)
...
end subroutine foo
以后我想这样称呼它
real :: rnumbers(40)
...
call foo(rnumbers, 20)
但是,我得到编译器错误:
error #6633: The type of the actual argument differs from the type of the dummy argument.
当然,这是可以理解的,因为真正的数组不是复杂的数组。但必须有办法让它发挥作用。
因为如果子例程foo
和foo
的调用在不同的模块中,写在不同的Fortran文件中,那么编译器不会 投诉,一切正常
有人知道如何让它工作吗?如果需要复杂数组,如何传递真实数组?
你可以使用transfer(rnumbers, ...)
来转换类型(很可能会创建一个临时数组)或者使用等价来避免它
real :: rnumbers(40)
complex :: cnumbers(20)
equivalence (rnumbers, cnumbers)
set the value of rnumbers
call foo(cnumbers, 20)
如果您需要可分配数组,则等效项将不起作用。
您还可以使用外部子例程,让编译器在接口方面撒谎,只传递真正的数组而不是复杂的数组。它不符合标准,但有时会使用。另见 Gfortran complex actual to real dummy argument
我有一个 Fortran 子例程,它需要一个像
这样的复杂数组subroutine foo(cnumbers, n)
integer :: n
complex :: cnumbers(n)
...
end subroutine foo
以后我想这样称呼它
real :: rnumbers(40)
...
call foo(rnumbers, 20)
但是,我得到编译器错误:
error #6633: The type of the actual argument differs from the type of the dummy argument.
当然,这是可以理解的,因为真正的数组不是复杂的数组。但必须有办法让它发挥作用。
因为如果子例程foo
和foo
的调用在不同的模块中,写在不同的Fortran文件中,那么编译器不会 投诉,一切正常
有人知道如何让它工作吗?如果需要复杂数组,如何传递真实数组?
你可以使用transfer(rnumbers, ...)
来转换类型(很可能会创建一个临时数组)或者使用等价来避免它
real :: rnumbers(40)
complex :: cnumbers(20)
equivalence (rnumbers, cnumbers)
set the value of rnumbers
call foo(cnumbers, 20)
如果您需要可分配数组,则等效项将不起作用。
您还可以使用外部子例程,让编译器在接口方面撒谎,只传递真正的数组而不是复杂的数组。它不符合标准,但有时会使用。另见 Gfortran complex actual to real dummy argument