假设我有一个带有以下API的C函数:
int c_function(int **a);
假设C函数负责内存分配,我应该如何声明Fortran数组/指向数组并将其传递给函数?
答案 0 :(得分:3)
你必须声明一个type(c_ptr)
type(c_ptr) :: ptr
然后调用你的函数(使用适当的接口)
n = c_function(ptr)
然后只指向那里的Fortran数组指针
real, pointer :: Array(:)
call c_f_pointer(ptr, Array, [n])
我假设n
是数组的大小。你必须知道它,否则就不可能了。
界面看起来像
interface
integer(c_int) function c_function(ptr) bind(C,name="c_function")
use iso_c_binding
type(c_ptr) :: ptr
end function
end interface