Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 509
Dooggy Avatar asked Sep 28 '22 18:09

Dooggy


1 Answers

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
like image 90
Vladimir F Героям слава Avatar answered Nov 12 '22 23:11

Vladimir F Героям слава