Suppose that I have a C function with the following API :
int c_function(int **a);
How should I go about declaring a Fortran array/pointer to array, and passing it to the function, assuming that the C function is responsible for memory allocation ?
You must declare a Fortan c-pointer of type(c_ptr)
type(c_ptr) :: ptr
then call your function (with proper interface)
n = c_function(ptr)
and only then point a Fortran array pointer there
real, pointer :: Array(:)
call c_f_pointer(ptr, Array, [n])
I assumed that n
is the size of the array. You must know it, otherwise it is impossible.
The interface could look like
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With