return a device pointer from a C function call back to Fortr

I have a C functions, that allocate memory on CUDA device, and then return back the pointer to that device memory. There are two possible ways for this C functions:

  1. a function with a double** argument
  2. a function return double*

It’s easy to implement in C. However, as I read that Fortran is not easy to handle pointer-to-pointer, I chose the second one. For the second interface, I tried using type(c_ptr), yet it doesn’t work

in C:

double* foofuunc() {...}

Fortran interface:

type(c_ptr) foofunc() bind(c)

and then I try to cast to a device memory variable in Fortran, declared with

double precision, dimension(:), allocatable, device :: a

I got “illegal number of type of argument”

type(c_ptr) :: c_a
c_f_pointer(c_a, a)

Could someone give me an advice.


Any one can help me, please.