In many C or C + + header file definitions, NULL is specified to be defined as 0, which is no longer specifically expanded
Gfortran's Manual for the section on ISO c binding, define null as follows
Moreover, the following and named constants are defined:
Name |
Type |
C_NULL_PTR |
C_PTR |
C_NULL_FUNPTR |
C_FUNPTR |
Both is equivalent to the value in NULL
C.
Thus, it is necessary to pass a null pointer from Fortran to C or C + +, which requires that the incoming parameter be set to the pointer type C_ptr
Next is the code:
#include"stdio.h"intTestint*a,intnum) { if(!a) {printf ("This is null pointer\n"); } Else{ for(intI=0; i<num; i++) {printf ("array[%d]=%d\n", i+1, A[i]); } } return 0;}
Program Mainuse, intrinsic:: iso_c_bindingImplicitNoneInterfaceinteger (c_int) function func (array, n) bind (c, name="Test") ImportImplicitNone Integer (c_int), Intent (inch), Value:: N Integer (C_int), Intent (inch):: Array (n) End Function integer (c_int) function Func2 (PT, N) bind (c, name="Test") ImportImplicitNone Integer (c_int), Intent (inch), Value:: N Type (c_ptr), Value:: Pt end FunctionEndInterfacetype (c_ptr):: A1integer (C_int), Target:: ABC (5) Integer:: Iabc= [ A, A, *, the, One]A1= C_loc (ABC (1))!Pass Array Directlyi=func (ABC, size (ABC))!Pass Addressi= Func2 (C_loc (ABC (1)),5)! PassNULLPointeri= Func2 (C_null_ptr,5)! PassNULLfunction Pointeri= Func2 (C_null_funptr,5) End Program
Gfortran code_c.c code_for.f90-o Test
Operation Result:
array[1]=12array[2]=12array[3]=35array[4]=67array[5]=11
array[1]=12array[2]=12array[3]=35array[4]=67array[5]=11
This is null pointer
This is null pointer
FORTRAN passes null values to C