Procedures for binding to C interfaces#

c_associated#

Name#

c_associated(3) - [ISO_C_BINDING] Status of a C pointer

Syntax#

result = c_associated(c_prt_1, c_ptr_2)

Description#

c_associated(c_prt_1[, c_ptr_2]) determines the status of the C pointer c_ptr_1 or if c_ptr_1 is associated with the target c_ptr_2.

Arguments#

  • c_ptr_1

    Scalar of the type c_ptr or c_funptr.

  • c_ptr_2

    (Optional) Scalar of the same type as c_ptr_1.

Returns#

The return value is of type logical; it is .false. if either c_ptr_1 is a C NULL pointer or if c_ptr1 and c_ptr_2 point to different addresses.

Examples#

Sample program:

program demo_c_associated

contains

subroutine association_test(a,b)
use iso_c_binding, only: c_associated, c_loc, c_ptr
implicit none
real, pointer :: a
type(c_ptr) :: b
   if(c_associated(b, c_loc(a))) &
      stop 'b and a do not point to same target'
end subroutine association_test

end program demo_c_associated

Standard#

Fortran 2003 and later

See Also#

c_loc(3), c_funloc(3), iso_c_binding(3)

fortran-lang intrinsic descriptions#

c_f_pointer#

Name#

c_f_pointer(3) - [ISO_C_BINDING] Convert C into Fortran pointer

Syntax#

call c_f_pointer(cptr, fptr, shape)

Description#

c_f_pointer(cptr, fptr[, shape]) Assign the target, the C pointer, cptr to the Fortran pointer fptr and specify its shape.

Arguments#

  • cptr

    scalar of the type c_ptr. It is intent(in).

  • fptr

    pointer interoperable with cptr. it is intent(out).

  • shape

    (Optional) Rank-one array of type integer with intent(in) . It shall be present if and only if fptr is an array. The size must be equal to the rank of fptr.

Examples#

Sample program:

program demo_c_f_pointer
use iso_c_binding
implicit none
interface
   subroutine my_routine(p) bind(c,name='myC_func')
      import :: c_ptr
      type(c_ptr), intent(out) :: p
   end subroutine
end interface
type(c_ptr) :: cptr
real,pointer :: a(:)
   call my_routine(cptr)
   call c_f_pointer(cptr, a, [12])
end program demo_c_f_pointer

Standard#

Fortran 2003 and later

See Also#

c_loc(3), c_f_procpointer(3), iso_c_binding(3)

fortran-lang intrinsic descriptions#

c_f_procpointer#

Name#

c_f_procpointer(3) - [ISO_C_BINDING] Convert C into Fortran procedure pointer

Syntax#

call c_f_procpointer(cptr, fptr)

Description#

c_f_procpointer(cptr, fptr) assigns the target of the C function pointer cptr to the Fortran procedure pointer fptr.

Arguments#

  • cptr

    scalar of the type c_funptr. It is intent(in).

  • fptr

    procedure pointer interoperable with cptr. It is intent(out).

Examples#

Sample program:

program demo_c_f_procpointer
use iso_c_binding
implicit none
abstract interface
   function func(a)
   import :: c_float
   real(c_float), intent(in) :: a
   real(c_float) :: func
   end function
end interface
interface
   function getIterFunc() bind(c,name="getIterFunc")
   import :: c_funptr
   type(c_funptr) :: getIterFunc
   end function
end interface
type(c_funptr) :: cfunptr
procedure(func), pointer :: myFunc
   cfunptr = getIterFunc()
   call c_f_procpointer(cfunptr, myFunc)
end program demo_c_f_procpointer

Standard#

Fortran 2003 and later

See Also#

c_loc(3), c_f_pointer(3), iso_c_binding(3)

fortran-lang intrinsic descriptions#

c_funloc#

Name#

c_funloc(3) - [ISO_C_BINDING] Obtain the C address of a procedure

Syntax#

result = c_funloc(x)

Description#

c_funloc(x) determines the C address of the argument.

Arguments#

  • x

    Interoperable function or pointer to such function.

Returns#

The return value is of type c_funptr and contains the C address of the argument.

Examples#

Sample program:

! program demo_c_funloc and module
module x
use iso_c_binding
implicit none
contains
subroutine sub(a) bind(c)
real(c_float) :: a
   a = sqrt(a)+5.0
end subroutine sub
end module x
!
program demo_c_funloc
use iso_c_binding
use x
implicit none
interface
   subroutine my_routine(p) bind(c,name='myC_func')
     import :: c_funptr
     type(c_funptr), intent(in) :: p
   end subroutine
end interface
   call my_routine(c_funloc(sub))
!
end program demo_c_funloc

Standard#

Fortran 2003 and later

See Also#

c_associated(3), c_loc(3), c_f_pointer(3),

c_f_procpointer(3), iso_c_binding(3)

fortran-lang intrinsic descriptions#

c_loc#

Name#

c_loc(3) - [ISO_C_BINDING] Obtain the C address of an object

Syntax#

result = c_loc(x)

Description#

c_loc(x) determines the C address of the argument.

Arguments#

  • x

    Shall have either the pointer or target attribute. It shall not be a coindexed object. It shall either be a variable with interoperable type and kind type parameters, or be a scalar, nonpolymorphic variable with no length type parameters.

Returns#

The return value is of type c_ptr and contains the C address of the argument.

Examples#

Sample program:

   subroutine association_test(a,b)
   use iso_c_binding, only: c_associated, c_loc, c_ptr
   implicit none
   real, pointer :: a
   type(c_ptr) :: b
     if(c_associated(b, c_loc(a))) &
        stop 'b and a do not point to same target'
   end subroutine association_test

Standard#

Fortran 2003 and later

See Also#

c_associated(3), c_funloc(3), c_f_pointer(3),

c_f_procpointer(3), iso_c_binding(3)

fortran-lang intrinsic descriptions#

c_sizeof#

Name#

c_sizeof(3) - [ISO_C_BINDING] Size in bytes of an expression

Syntax#

n = c_sizeof(x)

Description#

c_sizeof(x) calculates the number of bytes of storage the expression x occupies.

Arguments#

  • x

    The argument shall be an interoperable data entity.

Returns#

The return value is of type integer and of the system-dependent kind csize_t (from the _iso_c_binding module). Its value is the number of bytes occupied by the argument. If the argument has the pointer attribute, the number of bytes of the storage area pointed to is returned. If the argument is of a derived type with pointer or allocatable components, the return value does not account for the sizes of the data pointed to by these components.

Examples#

Sample program:

program demo_c_sizeof
use iso_c_binding
implicit none
real(c_float) :: r, s(5)
   print *, (c_sizeof(s)/c_sizeof(r) == 5)
end program demo_c_sizeof

Results:

    T

The example will print .true. unless you are using a platform where default real variables are unusually padded.

Standard#

Fortran 2008

See Also#

storage_size(3)

fortran-lang intrinsic descriptions#