【问题标题】:How to compare Fortran c_ptr with null如何将 Fortran c_ptr 与 null 进行比较
【发布时间】:2015-08-24 05:13:56
【问题描述】:

我有一些需要合并的 Fortran 和 C 代码。

我使用的 Fortran 界面大致如下所示:

module bridge
  use, intrinsic::iso_c_binding, only : c_ptr, c_null_ptr
  implicit none

  type(c_ptr) :: instance

  interface

    function c_init() result(this) bind(C, name="bridge_init")
      import
      type(c_ptr) :: this
    end function c_init

  end interface

contains

    subroutine init() 
      instance = c_init()
    end subroutine init

end module bridge

我的问题是我想在init 子例程中检查初始化,类似于

subroutine init()
  if( instance .eq. c_null_ptr ) then
    instance = c_init()
  end if
end subroutine

但这给了我一个Syntax error, found END-OF-STATEMENT when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE INTEGER REAL COMPLEX BYTE CHARACTER CLASS,然后是This binary operation is invalid for this data type.

那我应该改用什么?

【问题讨论】:

    标签: fortran fortran-iso-c-binding


    【解决方案1】:

    您只需要使用 iso_c_binding 内部模块中的 c_associated 函数。它使用一个参数检查是否为空

    subroutine init() 
      if( .not. c_associated(instance) ) then
        instance = c_init()
      end if
    end subroutine init
    

    【讨论】:

    • 其实和普通的 Fortran 指针是一样的。你也不和null()比较,但你打电话给associated()
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多