7.1.5. Working with Pointers

7.1.5 Working with Pointers

C pointers are represented in Fortran via the special opaque derived type type(c_ptr) (with private components). Thus one needs to use intrinsic conversion procedures to convert from or to C pointers.

For some applications, using an assumed type (TYPE(*)) can be an alternative to a C pointer; see Further Interoperability of Fortran with C.

For example,

use iso_c_binding
type(c_ptr) :: cptr1, cptr2
integer, target :: array(7), scalar
integer, pointer :: pa(:), ps
cptr1 = c_loc(array(1)) ! The programmer needs to ensure that the
                        ! array is contiguous if required by the C
                        ! procedure
cptr2 = c_loc(scalar)
call c_f_pointer(cptr2, ps)