FORTRAN passes null values to C

Source: Internet
Author: User
Tags define null gfortran

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.