I am trying to pass a single dimension array from a FORTRAN program to C.
The C function is called but the values that it holds are garbage. But whereas if I try calling the same function using an integer variable I am able to pass the required value. Can anyone help me out with this?
The code I am using is similar to this
File: fortran_prog.f
program test
real*4 :: a(4)
data a / 1,2,3,4 /
call test_func(a)
end program test
File: c_prog.c
int test_func(double a[]) {
int i;
for(i=0;i<4;i++) {
printf("%f\n",a[i]);
}
return 0;
}
Passing arrays between Fortran and C is a non-trivial problem. The particular C and Fortran compilers matter.
The first problem I see is that you specify
double
to matchreal*4
. That is certainly invalid on almost all platforms. Declare the C function as:This could work on some platforms, though many Fortran compilers pass the address of an "array descriptor" and not the array itself. Check the documentation for the Fortran compiler.