I have a problem where I am calling a C function using from a FORTRAN program.
I situation is I am passing a variable's address as a parameter from FORTRAN and using it in C and I return the same variable after doing some computation.
In the C function when I declare the parameter as a pointer then I am able to get the required value of the variable in the FORTRAN program but it shows a Segmentation Fault.
But whereas if I declare the parameter as a normal variable then I am unable to pass the value back to FORTRAN
An example would be
File: fortran_prog.f
program test
integer :: a=10
call c_func(a)
write(*,*) a
end program test
File: c_prog1.c
#include<stdio.h>
void c_func_(int *a) {
int *b = 100;
*a = *b
}
In this case I get the value of 'a' as 100 but it also shows Segmentation Fault after the FORTRAN program exits.
But if I change the C program like this
File: c_prog2.c
#include<stdio.h>
void c_func_(int *a) {
int b = 100;
a = &b;
}
In this case I get the value of a as 0 in the FORTRAN program. I couldn't find out the reason for this behavior
The C prog is compiled with gcc and FORTRAN program with gfortran. When used GDB to back trace I got the error message
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
Any help/suggestion would be appreciated.
PS: The above examples are merely replicating the actual program's code.