Dooggy
Dooggy

Reputation: 330

Calling C function with **int parameter from Fortran

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 ?

Upvotes: 5

Views: 167

Answers (1)

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

Upvotes: 4

Related Questions