将Fortran的二维数组传递给C.

我无法将Fortran中的二维数组传递给C.以下是我的C函数,它只是在屏幕上显示数组元素。

#include  void print2(double *arr , int *n) { int y = *n; printf("\ny = %d", y); for(int i =0; i<y; i++) { for (int j = 0; j < y; j++) printf("%.6g", *((arr + i*y) + j)); printf("\n"); } } 

到目前为止,我的Fortran代码如下:

 program linkFwithC use, intrinsic :: iso_c_binding implicit none real, dimension(3,3)::a a(1,1)=1 a(1,2)=2 a(1,3)=3 a(2,1)=4 a(2,2)=5 a(2,3)=6 a(3,1)=7 a(3,2)=8 a(3,3)=9 interface subroutine print2(a,n) bind( c ) use, intrinsic :: iso_c_binding type(c_ptr)::a integer(C_INT)::n end subroutine print2 end interface call print2(c_loc(a),3) end program linkFwithC 

我链接两个文件的方法是为C函数创建一个静态库并构建.lib文件。 构建.lib文件后,我将其添加到fortran项目并运行fortran项目。 代码运行时没有错误,n值正确显示; 但是,显示的数组值都是错误的。

请帮忙 !

谢谢,阿纳斯

代码中存在[当前]显示的一些问题。

  • print2的Fortran接口中的n参数没有VALUE属性,但C函数中的相应参数是按值获取的。 考虑将VALUE添加到Fortran声明中。

  • 指向数组的指针也出现了同样的问题。 Fortran接口传递没有值的指针,C函数需要“按值指针”(而不是指向指针的指针)。 请注意,这里不需要显式使用C_PTR – 您可以使用实际的数组类型构建可互操作的接口。

  • 在大多数平台上,Fortran默认REAL与C double不同 – 考虑使用ISO_C_BINDING中的类型常量来确保Fortran端的REAL类型与C的类型匹配。

  • C_LOC要求其参数具有TARGET属性。 将该属性添加到主程序中变量的声明中。

经过进一步的研究,我能够找到一个相关的工作如下:

以下是我的C函数:

 #include  void print2(void *p, int n) { printf("Array from C is \n"); double *dptr; dptr = (double *)p; for (int i = 0; i < n; i++) { for (int j = 0; j 

以下是我的Fortran代码:

 program linkFwithC use iso_c_binding implicit none interface subroutine my_routine(p,r) bind(c,name='print2') import :: c_ptr import :: c_int type(c_ptr), value :: p integer(c_int), value :: r end subroutine end interface integer,parameter ::n=3 real (c_double), allocatable, target :: xyz(:,:) real (c_double), target :: abc(3,3) type(c_ptr) :: cptr allocate(xyz(n,n)) cptr = c_loc(xyz(1,1)) !Inputing array valyes xyz(1,1)= 1 xyz(1,2)= 2 xyz(1,3)= 3 xyz(2,1)= 4 xyz(2,2)= 5 xyz(2,3)= 6 xyz(3,1)= 7 xyz(3,2)= 8 xyz(3,3)= 9 call my_routine(cptr,n) deallocate(xyz) pause end program linkFwithC