How to compare Fortran c_ptr with zero

I have Fortran and C code to combine.

I use the Fortran interface, which looks basically like this:

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 

My problem is that I would like to put a check for initialization in the init routine, something like

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

But it gives me a Syntax error, found END-OF-STATEMENT when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE INTEGER REAL COMPLEX BYTE CHARACTER CLASS followed by This binary operation is invalid for this data type.

So what should I use instead?

+4
source share
1 answer

You just need to use the c_associated function from the c_associated built-in module. With one argument, it checks for null

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

All Articles