Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

C funtion call from fortran

cvnravi
Beginner
590 Views
Hi,
I have written a funtion in C language. I am trying to call it from fortran(2003).

ex: fun(int a,int b) -- here a is input to the funtion and b is output to the funtion.

I am calling the above funtion from fortran by giving some input to 'a'. It is processing fine in C language and filled output value 'b'. But when it comes to fortran file. Output value for 'b' is not holding the data. its giving some garbage. How can I get the output value?
0 Kudos
2 Replies
Nick2
New Contributor I
590 Views
Quoting - cvnravi
Hi,
I have written a funtion in C language. I am trying to call it from fortran(2003).

ex: fun(int a,int b) -- here a is input to the funtion and b is output to the funtion.

I am calling the above funtion from fortran by giving some input to 'a'. It is processing fine in C language and filled output value 'b'. But when it comes to fortran file. Output value for 'b' is not holding the data. its giving some garbage. How can I get the output value?

If I had to guess without you posting an actual code sample,

1) (more likely mistake) make sure that your C declaration is
void fun(int&a,int&b); // by reference not by value
2) (less likely mistake) make sure you use the same size variables;
integer*4 a,b !Fortran declaration that corresponds to int type in C
3) (more likely mistake) Your INTERFACE declaration has INTENT::IN and INTENT::OUT (or similar) attributes...are you sure you got those right?
0 Kudos
cvnravi
Beginner
590 Views

If I had to guess without you posting an actual code sample,

1) (more likely mistake) make sure that your C declaration is
void fun(int&a,int&b); // by reference not by value
2) (less likely mistake) make sure you use the same size variables;
integer*4 a,b !Fortran declaration that corresponds to int type in C
3) (more likely mistake) Your INTERFACE declaration has INTENT::IN and INTENT::OUT (or similar) attributes...are you sure you got those right?

I am using C language not c++. So I cannot use references. I am using pointer in the declaration and definetion of the C funtion.

please check the below code block from 'C'


DWORD MorphoData(unsigned long clientindiex,float latitude,float longitude,float hmin,int elev_interpol,int lulc_vote,
int trace,float *elevation,int *lulc,float *tree_den,int *tree_height)
{
int err;

morph_input mor_in={40,-104,hmin,elev_interpol,lulc_vote,trace};
morph_output *mor_out = (void*) malloc(sizeof(morph_output));
err = connectToDB();
if(err)
printf("DB connection Failed");

err = morpho_data(mor_in,mor_out);

*elevation = mor_out->elevation;
*lulc = mor_out->lulc;
*tree_den = mor_out->tree_den;
*tree_height = mor_out->tree_height;

free(mor_out);
err = CloseConnection();
0 Kudos
Reply