C函数应该是一个三维数组,你可能需要构建一个指针链给他用。
[C] 纯文本查看 复制代码 #include <stdio.h>
int get_table(char ***Results){
Results[0][0][0] = 'f';
Results[0][0][1] = 'c';
Results[0][0][2] = 'o';
Results[0][0][3] = 'd';
Results[0][0][4] = 'e';
Results[3][5][0] = 'f';
Results[3][5][1] = 'c';
Results[3][5][2] = 'o';
Results[3][5][3] = 'd';
Results[3][5][4] = 'e';
return 1;
}
[Fortran] 纯文本查看 复制代码 Program Main
use ISO_C_Binding
use CLangMultiDimensionArrayLink2D
implicit none
interface
integer(c_int) function get_table(val) bind(c)
import
type(c_ptr) , value :: val
end function
end interface
Integer , parameter :: M = 6 , N = 4
character(8) ,target :: Results(M,N)
integer :: i
type(CLang2DArrayLink) :: linkResults
i = get_table( linkResults%Set(Results) )
write(*,"(2(a8,1x))") Results(1,1),Results(M,N)
End Program Main
以下模块可以复制粘贴,不做修改。
[Fortran] 纯文本查看 复制代码 Module CLangMultiDimensionArrayLink2D
use , Intrinsic :: ISO_C_Binding
Implicit None
private
Type , public :: CLang2DArrayLink
type(c_ptr) , pointer :: p1(:) , p2(:,:)
contains
Procedure :: Set
Final :: Release
End Type CLang2DArrayLink
contains
Type(c_ptr) Function Set( this , datas ) result ( cptr )
class(CLang2DArrayLink) :: this
class(*) , intent(IN) :: datas(1:,1:)
integer :: sizes(2) , i , j
sizes = shape(datas)
if( associated(this%p1) ) deallocate(this%p1,this%p2)
Allocate(this%p1(sizes(2)),this%p2(sizes(1),sizes(2)))
Do i = 1 , sizes(2)
Do j = 1 , sizes(1)
this%p2(j,i) = c_loc(datas(j,i))
End Do
this%p1(i) = c_loc(this%p2(1,i))
End Do
cptr = c_loc(this%p1(1))
End Function Set
Subroutine Release( this )
type(CLang2DArrayLink) :: this
if( associated(this%p1) ) deallocate(this%p1,this%p2)
End Subroutine Release
End Module CLangMultiDimensionArrayLink2D
|