绑定到 C 接口的过程#

c_associated#

名称#

c_associated(3) - [ISO_C_BINDING] C 指针的状态

Synopsis#

    result = c_associated(c_prt_1, [c_ptr_2] )
     logical function c_associated(c_prt_1, cptr_2)

      TYPE,intent(in) ::c_ptr_1
      TYPE,intent(in),optional ::c_ptr_2

Characteristics#

  • c_ptr_1 is a scalar of the type c_ptr or c_funptr.

  • c_ptr_2 is a scalar of the same type as c_ptr_1.

  • The return value is of type logical

说明#

c_associated(3) determines the status of the C pointer c_ptr_1 or if c_ptr_1 is associated with the target c_ptr_2.

选项#

  • c_ptr_1

    C pointer to test for being a C NULL pointer, or to test if pointing to the same association as c_ptr_2 when present.

  • c_ptr_2

    C pointer to test for shared association with c_ptr_1

结果#

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.

示例#

示例程序:

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

标准#

Fortran 2003

另见#

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

fortran-lang intrinsic descriptions

c_f_pointer#

名称#

c_f_pointer(3) - [ISO_C_BINDING] 将 C 指针转换为 Fortran 指针

Synopsis#

    call c_f_pointer(cptr, fptr [,shape] )
     subroutine c_f_pointer(cptr, fptr ,shape )

      type(c_ptr),intent(in) :: cprt
      type(TYPE),pointer,intent(out) :: fprt
      integer,intent(in),optional :: shape(:)

Characteristics#

The Fortran pointer fprt must be interoperable with cptr

shape is only specified if fptr is an array.

说明#

c_f_pointer(3) assigns the target (the C pointer cptr) to the Fortran pointer fptr and specifies its shape if fptr points to an array.

选项#

  • cptr

    c_ptr 类型的标量。它是 intent(in) 的。

  • fptr

    可与 cptr 互操作的指针。它是 intent(out) 的。

  • shape

    (可选)具有 intent(in) 类型的 integer 一维数组。当且仅当 fptr 是一个数组时,它才会出现。大小必须等于 fptr 的维度。

示例#

示例程序:

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

标准#

Fortran 2003

另见#

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

fortran-lang intrinsic descriptions

c_f_procpointer#

名称#

c_f_procpointer(3) - [ISO_C_BINDING] 将 C 函数指针转换为 Fortran 过程指针

Synopsis#

    call c_f_procpointer(cptr, fptr)
     subroutine c_f_procpointer(cptr, fptr )

      type(c_funptr),intent(in) :: cprt
      type(TYPE),pointer,intent(out) :: fprt

Characteristics#

说明#

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

选项#

  • cptr

    c_funptr 类型的标量。它是**intent(in)**的。

  • fptr

    可与 cptr 互操作的过程指针。它是 intent(out)

示例#

示例程序:

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

标准#

Fortran 2003

另见#

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

fortran-lang intrinsic descriptions

c_funloc#

名称#

c_funloc(3) - [ISO_C_BINDING] 获取一个过程的C地址

Synopsis#

    result = c_funloc(x)

Characteristics#

说明#

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

选项#

  • x

    可互操作的函数或指向此类函数的指针。

结果#

返回值是 c_funptr 类型,包含参数的 C 地址。

示例#

示例程序:

! 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

标准#

Fortran 2003

另见#

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

c_f_procpointer(3)iso_c_binding(3)

fortran-lang intrinsic descriptions

c_loc#

名称#

c_loc(3) - [ISO_C_BINDING] 获取对象的C地址

Synopsis#

    result = c_loc(x)

Characteristics#

说明#

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

选项#

  • x

    应具有 pointertarget 属性。它不应是一个共同索引的对象。它应该是一个具有可互操作类型和种类类型参数的变量,或者是一个没有长度类型参数的标量、非多态变量。

结果#

返回值是 c_ptr 类型,包含参数的 C 地址。

示例#

示例程序:

   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

标准#

Fortran 2003

另见#

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

c_f_procpointer(3)iso_c_binding(3)

fortran-lang intrinsic descriptions

c_sizeof#

名称#

c_sizeof(3) - [ISO_C_BINDING] 表达式的字节大小

Synopsis#

    result = c_sizeof(x)

Characteristics#

说明#

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

选项#

  • x

    参数应该是一个可互操作的数据实体。

结果#

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.

示例#

示例程序:

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

结果:

    T

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

标准#

Fortran 2008

另见#

storage_size(3)

fortran-lang intrinsic descriptions