本帖最后由 pasuka 于 2015-8-31 19:58 编辑
按照gfortran手册的说法
http://gcc.gnu.org/onlinedocs/gfortran/ISO_005fC_005fBINDING.html
直接用下面两个中任意一个都可以的
Moreover, the following two named constants are defined:
Name Type
C_NULL_PTR C_PTR
C_NULL_FUNPTR C_FUNPTR
Both are equivalent to the value NULL in C.代码参考
[C] 纯文本查看 复制代码 #include "stdio.h"
int test(int *a, int num)
{
if(!a){
printf("This is null pointer\n");
}
else{
for(int i=0; i<num; i++){
printf("Array[%d]=%d\n", i+1, a[i]);
}
}
return 0;
}
[Fortran] 纯文本查看 复制代码 program main
use, intrinsic:: iso_c_binding
implicit none
interface
integer(c_int) function func(array, n) bind(c, name="test")
import
implicit none
integer(c_int), intent(in), value:: n
integer(c_int), intent(in):: array(n)
end function
integer(c_int) function func2(pt, n) bind(c, name="test")
import
implicit none
integer(c_int), intent(in), value:: n
type(c_ptr), value:: pt
end function
end interface
type(c_ptr) :: a1
integer(c_int), target:: abc(5)
integer:: i
abc = [12, 12, 35, 67, 11]
a1 = c_loc(abc(1))
! Pass array directly
i = func(abc, size(abc))
! Pass address
i = func2(c_loc(abc(1)), 5)
! Pass null pointer
i = func2(c_null_ptr, 5)
! Pass null function pointer
i = func2(c_null_funptr, 5)
end program
|