在很多C或C++的头文件定义中,NULL被指定定义为0,这里不再具体展开

gfortran的手册关于iso c binding的章节,定义NULL如下

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.

据此,从Fortran向C或C++传递空指针还是需要一些技巧,也即需要把传入参数设置为指针类型C_PTR

接下来是代码:

#include "stdio.h"

int test(int *a, int num)
{
if(!a){
printf("This is null pointer\n");
}
else{
for(int i=; i<num; i++){
printf("Array[%d]=%d\n", i+, a[i]);
}
}
return ;
}
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()
integer:: i abc = [, , , , ]
a1 = c_loc(abc())
! Pass array directly
i = func(abc, size(abc))
! Pass address
i = func2(c_loc(abc()), )
! Pass null pointer
i = func2(c_null_ptr, )
! Pass null function pointer
i = func2(c_null_funptr, )
end program
gfortran code_c.c code_for.f90 -o test

运行结果:

Array[1]=12
Array[2]=12
Array[3]=35
Array[4]=67
Array[5]=11
Array[1]=12
Array[2]=12
Array[3]=35
Array[4]=67
Array[5]=11
This is null pointer
This is null pointer

  

Fortran向C传递NULL值的更多相关文章

  1. c# 传递Null的string值导致的调用C++的dll报错 Attempted to read or write protected memory.

    c# 调用C++的dll报错 Attempted to read or write protected memory:   原因是:c# 传递Null的string值导致的,将Null改为string ...

  2. SQL SERVER使用ODBC 驱动建立的链接服务器调用存储过程时参数不能为NULL值

    我们知道SQL SERVER建立链接服务器(Linked Server)可以选择的驱动程序非常多,最近发现使用ODBC 的 Microsoft OLE DB 驱动程序建立的链接服务器(Linked S ...

  3. javascript中值传递与值引用的研究

    今天重新看了一下<javascript高级程序设计>,其中讲到了javascript中的值传递和值引用,所以就自己研读了一下,但是刚开始没有明白函数中的参数只有值传递,有的场景好像参数是以 ...

  4. hibernate的dao中参数的传递取值

    hibernate的dao中参数的传递取值 private Query setParameter(Query query, Map<String, Object> map) { if (m ...

  5. Swift:消除Null值

    由于在现代编程语言中这个无所不在的概念,许多程序猿可能倾向于相信null值是一个必须存在的瑕疵,创建一个没有它的编程语言是不可能的.他们可能会惊奇那些许多没有null值活的也很好的语言,这带来的结果就 ...

  6. 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结

    为什么说JAVA中要慎重使用继承   这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...

  7. oracle 关于null值排序

    在oracle中根据字段来desc排序的话null值可能会在数据的最前面.然而有时候我们查看数据的时候并不希望能够在前面看到这些null值的排序数据. 因此我查了一下: 1.排序的时候运用nvl(). ...

  8. SQL中NULL值

    SQL的表达式,除了IS NULL和NOT NULL以外,只要出现NULL值结果都为FALSE 简单的例子: SELECT * FROM table WHERE name!='abc' 只要name值 ...

  9. 关于null值的排序

    关于空值null的排序问题   Oracle排序中NULL值处理的五种常用方法: 1.缺省Oracle在Order by 时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在 ...

随机推荐

  1. java资源下载之官网地址

    [一].json下载地址 http://sourceforge.net/projects/json-lib/files/ [二].apache-commons下载地址 http://commons.a ...

  2. 三种实例化委托的方式(C# 编程指南)

    1.定义的委托和方法 delegate void TestDelegate(string s); static void M(string s) { Console.WriteLine(s); } 2 ...

  3. Maven(一)简介和基本安装使用

    简介 如今用于项目管理和自动化构建的东东用的比较多的,比如: eclipse中用到的ant 现今流行的android studio中用到的gradle 这里将介绍另一种工具——maven (也可以用来 ...

  4. windows网络编程的一些理论

    参考自<VC++深入详解> 这是我在看书时记录下来的东西. 注:下面的Socket其实都应该是socket 第14章网络编程 Socket是连接应用程序与网络驱动程序的桥梁,Socket在 ...

  5. QQ第三方登录

    QQ第三方登录 在Android应用程序的开发过程中,很多时候需要加入用户登录/注册模块.除了自己动手设计登录界面并实现相应功能外,现在还可以借助百度.腾讯等开发者平台提供的第三方账号登录模块.最近研 ...

  6. maven integration with eclipse 3.0.4 does not work with NTLM proxy

    Recently downloaded m2e(maven integration with eclipse). The version is 3.0.4. My environment is beh ...

  7. ansible 使用方法

    免密钥方式登陆: [root@yizhen ~]# ssh-keygen -t rsa[root@yizhen ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168 ...

  8. 从topcoder赚钱的方法

    1. 算法1.1  SRM 钱少($30左右),而且很难.1.2  Tournament 钱多($1000~$10000),太难~ 2. 设计和开发2.1  构件设计和开发 钱比较多($1000左右) ...

  9. 获取手机的gps定位

    只要手机有GPS模块,可以用HTML5的Geolocation接口获取 在HTML5中,geolocation作为navigator的一个属性出现,它本身是一个对象,拥有三个方法: - getCurr ...

  10. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...