KIND parameter with C interoperable

I have a data type defined using KIND parameter

INTEGER :: dp = selected_real_kind(P=15)
REAL(KIND=dp), dimension(10) :: arr

Now I want to pass this array to a C function that I call via iso_c_binding. I believe the interoparate type for double in C is real(c_double) in Fortran. However, this doesn’t have the same kind with the data I defined above. The compiler gives me the “type mismatch” error.

INTERFACE 
   integer(c_int) function c_foo_func (arr, size) bind(c) 
       real(c_double) :: arr
       integer(c_int), value :: size
   end
end interface

Could someone suggest me a solution for this.
Thanks
Tuan

Hi Tuan,

“arr” isn’t a real, it’s an array of real. So if you’re calling c_foo_func using just ‘arr’, then you should get a rank mismatch.

To fix, try adding a ‘dimension(*)’ in the interface:

INTERFACE
   integer(c_int) function c_foo_func (arr, size) bind(c)
       real(c_double), dimension(*) :: arr
       integer(c_int), value :: size
   end
end interface

Hope this helps,
Mat

Thanks, Mat

Tuan.