Fortran Coder

查看: 12492|回复: 6
打印 上一主题 下一主题

[混编] fortran的空指针问题(调用C函数)

[复制链接]

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

楼主
发表于 2015-8-31 19:12:48 | 显示全部楼层
本帖最后由 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

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-7 01:46

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表