Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

C_F_PROCPOINTER

Intrinsic Module Subroutine: Associates a Fortran procedure pointer with the target of a C function pointer.

Module

USE, INTRINSIC :: ISO_C_BINDING

Syntax

CALL C_F_PROCPOINTER(cptr, fptr)

cptr

(Input) Is a scalar of derived type C_FUNPTR. Its value is the C address of a procedure.

fptr

(Output) Is a Fortran procedure pointer. It becomes pointer-associated with the target of cptr.

Examples

Example 1

The following Fortran subroutine can be called from a C program that passes a pointer to a C function to be called:

SUBROUTINE CallIt (cp) BIND(C)
USE, INTRINSIC :: ISO_C_BINDING
TYPE(C_FUNPTR), INTENT(IN) :: cp
ABSTRACT INTERFACE
  SUBROUTINE Add_Int (i) BIND(C)
  IMPORT
  INTEGER(C_INT), INTENT(INOUT) :: i
  END SUBROUTINE Add_Int
END INTERFACE
PROCEDURE(Add_Int), POINTER :: fp
INTEGER(C_INT) :: j

CALL C_F_PROCPOINTER (cp, fp)
j = 1
CALL fp(j)
...

Example 2

In the following C code, a function pointer to the C function foo is passed to the Fortran subroutine bar. bar converts the function pointer to a procedure pointer, and calls foo through the converted procedure pointer.

main.c:
#include <stdio.h>

extern void bar_ (void (**) (int*));
extern void foo (int *a){
   printf ("  The value is %d\n", *a)
}

int main () {
   void (*fun_b)(int*) = NULL;
   int *i;
   int j = 10;

   fun_b = foo;
   i = &j;
   printf ("Test in C\n");
   fun_b (i);
   printf ("Test in Fortran\n");
   bar_ (&fun_b);
}

bar.f90
SUBROUTINE bar (c_fptr)
   USE ISO_C_BINDING
   TYPE(c_funptr)      :: c_fptr
   PROCEDURE(),POINTER :: proc_ptr
   INTEGER(c_int)      :: i = 20

   CALL c_f_procpointer (c_fptr, proc_ptr)
   CALL proc_ptr (i)

END SUBROUTINE bar

When these two files are compiled, linked, and executed, the output is:

Test in C
  The value is 10
Test in Fortran
  The value is 20