传递Fortran的整数数组到C子程序只有第一个元素传递 [英] Passing Fortran integer array to C subroutine only first element passed
问题描述
我想从Fortran的整数关口数组C,但我只能传递数组的第一个元素。
我的测试程序,低于该再现错误。我在哪里去了?
程序测试
使用富 整数(KIND = c_int的),可分配:: hgmu_dose(:)
分配(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打印*,HGMU_dose =,hgmu_dose电话test_interface(hgmu_dose)程序结束模块富
使用ISO_C_BINDING 隐无
接口
子程序test_interface(剂量)绑定(C,名字=接口)
进口:: c_int的
进口:: c_double
进口:: c_char 整数(KIND = c_int的),可分配::剂量(:)
结束子程序
结束接口前端模块富
通过
的#includeinterface.h 命名空间接口
{ 为externC无效接口(INT剂量[NDIM])
{
的for(int i = 0; I< NDIM;我++)
COUT<< 剂量[<< I<< ] =<<剂量[1] - ;&下; ENDL;
}
}
有几个问题在这里。
首先是在你的test_interface,接口的定义。你并不需要或想要的可分配
关键字。
子程序test_interface(剂量)绑定(C,名字=接口) 进口:: c_int的 整数(KIND = c_int的)::剂量(:)结束子程序
二是 ISO_C_BINDING
将通过引用传递。所以,当你用C定义函数应该接受指针数组:
无效接口(INT *剂量[NDIM])
最后一个侧面说明,你不必定义你的Fortran数组从0开始,您可以使用Fortran通常从1开始。
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 the test program below which reproduces the error. Where am I going wrong?
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;
}
}
There are a couple of issues here.
The first is in your definition of the test_interface, interface. You do not need or want the allocatable
keyword.
subroutine test_interface(dose) bind(C,name="INTERFACE")
import :: c_int
integer (kind=c_int) :: dose(:)
end subroutine
The second is the iso_c_binding
will pass by reference. So when you define your function in C it should be accepting a pointer to an array:
void INTERFACE (int *dose[NDIM])
Lastly as a side note, you don't have to define your Fortran array as starting from 0. You can use the Fortran normally starting from 1.
这篇关于传递Fortran的整数数组到C子程序只有第一个元素传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!