假设我有一个带有以下 API 的 C 函数:
int c_function(int **a);
假设 C 函数负责内存分配,我应该如何声明 Fortran 数组/数组指针,并将其传递给函数?
最佳答案
您必须声明 type(c_ptr)
type(c_ptr) :: ptr
然后调用您的函数(使用适当的接口(interface))
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
关于从 Fortran 调用带有 **int 参数的 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30595888/