I am calling a C function from a Fortran 90 program (I have to use Fortran 90). This C function takes a couple arguments and returns a float pointer. I cannot seem to print the returned data correctly in the Fortran code. It just displays a very larger number (which I assume is the address of the pointer.)
I have had success in passing REAL Fortran variables as arguments, having the C function set them (as Fortran passes by reference) and accessing the data then. However, I have to return the pointer as a return variable as this is the method a legacy function used (which I am re-implementing.)
Is there a way in Fortran 90 to access the data from a non-character (real, int, etc.) pointer returned from a C function? (Please note: I cannot use the ISO C bindings as that is only for Fortran 2003 and up.) I've put an idea of what I'm trying to do below...
Thanks!
Fortran Program
program test_real
real, dimension(10) :: realpt
integer nbr
integer i
real :: a=1.0
real :: b=2.0
real :: c=3.0
nbr = 9
realpt = getpointer(a, b, c)
do 10 i = 1, nbr
print *,"return: ",realpt(i)
10 continue
stop
End
C function
float* getpointer(float *a, float *b, float *c) {
float *rfl = (float *) calloc(9,sizeof(float));
int i=0;
for(i=0;i<3;i++) {
rfl[i] = *a;
}
for(i=3;i<6;i++) {
rfl[i] = *b;
}
for(i=6;i<9;i++) {
rfl[i] = *c;
}
return(rfl);
} // End of getpointer function
Output
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07
return: 3.1661344E+07