I'm trying to call a global string variable which is defined in a Fortran subroutine, in C. the C code is Cfile.c:
#include <stdio.h>
typedef struct {
        int length;
        char* string;
} fstring;
extern fstring stringf_;
void fortfunc_();
int main() {
        fstring stringC = stringf_;
        stringC.string[stringC.length-1] = '\0';
        printf("%s \n",stringC.string);
        return 0;
}
and FORTRAN code is Ffile.f:
subroutine fortfunc()
  
        character*30 string
        common/stringF/ string
        string = 'this is a string in FROTRAN77'
return
end
it compiles with:
gcc -c Cfile.c
gfortran -c -std=legacy Ffile.f
gfortran -c file.out -std=legacy Cfile.o Ffile.o
but when running I get the segmentation fault. I do not understand when I'm violating the memory boundaries though.
My operating system is:
Linux ubuntu 4.15.0-39-generic #42-Ubuntu SMP Tue Oct 23 15:48:01 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
and my compilers are:
GNU Fortran (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
I would appreciate if you could help me know where is my mistake and how I can solve it? other solutions to define a global variable in Fortran and then calling it in C are also welcomed.
 
                        
Based on the comments I got here and on Reddit, I now have a code which works. The C code:
and FORTRAN code:
The segfault was happening because the passed
stringC.lengthvalue is zero. It means unlike the example I was following here when calling a string from the FORTRAN side it does not pass the length as an integer!