我下面有一段代码,是想c指针转换为fortran指针,然后在c代码那边可以调用fortran的变量的值。代码如下: 我认为我的老师使用的c_f_pointer不对。 我想请教下大家应该如何将数组地址的值传递给 c中的void *addr使用。 简单的说是: c调用fortran,在fortran中有数组p需要传递数组每个地址给c代码的结构ptr_array中的void *addr,当addr得到地址后再使用地址进行相应的计算。 我应该如何获得数组p的地址,传递给c代码中的void *addr变量使用?并且需要传递计数的num变量给c代码使用? neighbor_addrs: type(ptr_array) ::neighbor_addrs args: type(calc1_type1),target :: args calc1_type: [td]type calc1_type1 | integer(c_int) ii,jj | end type calc1_type1[/td]摘去的部分代码: SUBROUTINE calc1_neighbor1(neighbor_addrs,args) use ISO_C_BINDING IMPLICIT NONE #include "aceMesh_runtime_f.h" type,bind(c) :: ptr_array integer :: len type(c_ptr) :: addr !在c中是定义的 void *addr类型 end real(c_double) src,src1 type(calc1_type1),target :: args integer(c_int) ii,jj,size,num type(ptr_array) ::neighbor_addrs !派生类型变量 type(c_ptr),pointer::array(:) !定义c指针 size=MAX_NEIGHBORS call c_f_pointer(neighbor_addrs%arr,array,[size]) !使用该转换函数使得c指针转换为f指针,然后将array数组指针传递给c代码使用 ii=args%ii jj=args%jj num=0 if(ii+iblock<=M)then num=num+1 array(num)=c_loc(p(ii+iblock,jj)) !获得fortran数组p的地址 endif neighbor_addrs%len=num !传递数量num给c使用 RETURN END SUBROUTINE calc1_neighbor1 我的想法是将fortran代码中的type(c_ptr) :: addr(该代码在c中是被定义为void *addr,注释:不能修改c代码),改为 integer(c_int) :: addr(32), 然后 array(num)=c_loc(p(ii+iblock,jj)) 改为 addr(num)=loc(p(ii+iblock,jj)),不知我的想法如何? |
If a pointer is a dummy-argument of an interoperable procedure, it usually has to be declared using the VALUE attribute. void* matches TYPE(C_PTR), VALUE, while TYPE(C_PTR) alone matches void**.
Name Type
C_NULL_PTR C_PTR
C_NULL_FUNPTR C_FUNPTR
Both are equivalent to the value NULL in C.
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |