4

I have a type with two pointers, one Fortran pointer and one C pointer:

type mytype
  procedure(whatever), pointer, nopass :: fortranPointer
  type(c_ptr) :: cPointer
end type 

I can assign something to both pointers like:

type(mytype) :: instance
instance%fortranPointer => fPtrToSomething
instance%cPointer = c_loc(somethingElse)

When I want to initialize the pointers without assigning something, I can use null() or nullify for the Fortran pointer:

instance%fortranPointer => null()

But how can I initialize the c_ptr to null? instance%cPointer = null() is not working as null() is not allowed on the right hand side of an assignement. c_loc(null()) is not allowed as null() is not a variable, which location can be obtained.

Is there something like a c_null?

1 Answer 1

3

Figured out that there is a intrinsic type named constant c_null_ptr that can be used.

Sign up to request clarification or add additional context in comments.

3 Comments

As a minor correction, c_null_ptr isn't an intrinsic type itself, but is a named constant of type c_ptr (so suitable for assignment and initialization for other variables of that type).
@francescalus Thanks. Wasn't aware of this internals.
Does one use pointer assignment or normal assignment to set a c_ptr to c_null_ptr?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.