我正在尝试将整数数组从 Fortran 传递给 C,但我只能传递数组的第一个元素。
我有下面的测试程序,它重现了错误。我哪里错了?
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
和
#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;
}
}