我有一些需要组合的 Fortran 和 C 代码。
我正在使用一个看起来像这样的 Fortran 接口(interface):
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.
那么我应该改用什么?
最佳答案
您只需要使用 c_associated
来自 iso_c_binding
的函数内在模块。它使用一个参数检查 null
subroutine init()
if( .not. c_associated(instance) ) then
instance = c_init()
end if
end subroutine init
关于fortran - 如何将 Fortran c_ptr 与 null 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32174843/