Passing an Fortran integer array to a C routine only the first element is passed

I am trying to pass an integer array from Fortran to C, but I can only pass the first element of the array.

I have a test program below which an error is reproduced. Where am I mistaken?

program test use foo integer (kind=c_int), allocatable :: hgmu_dose(:) allocate (hgmu_dose(0:10)) HGMU_dose(0)=22 HGMU_dose(1)=2 HGMU_dose(2)=3 HGMU_dose(3)=4 HGMU_dose(4)=5 HGMU_dose(5)=6 HGMU_dose(6)=7 HGMU_dose(7)=8 HGMU_dose(8)=9 HGMU_dose(9)=10 HGMU_dose(10)=11 print *, "HGMU_dose=", hgmu_dose call test_interface(hgmu_dose) end program module foo use ISO_C_Binding implicit none interface subroutine test_interface(dose) bind(C,name="INTERFACE") import :: c_int import :: c_double import :: c_char integer (kind=c_int), allocatable :: dose(:) end subroutine end interface end module foo 

WITH

 #include "interface.h" namespace interface { extern "C" void INTERFACE (int dose[NDIM]) { for (int i = 0; i < NDIM; i++) cout << " dose[" << i << "] = " << dose[i] << endl; } } 
0
source share
1 answer

There are a couple of issues here.

The first in your definition of the test_interface interface, the interface. You do not need or need the allocatable keyword.

 subroutine test_interface(dose) bind(C,name="INTERFACE") import :: c_int integer (kind=c_int) :: dose(:) end subroutine 

The second iso_c_binding will follow the link. Therefore, when you define your function in C, it should take a pointer to an array:

 void INTERFACE (int *dose[NDIM]) 

Finally, as an additional note, you do not need to define your Fortran array starting at 0. You can use Fortran, usually starting at 1.

0
source

Source: https://habr.com/ru/post/1239652/


All Articles