Skip to content

Instantly share code, notes, and snippets.

@kadircs
Last active July 3, 2020 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadircs/f66fa8490dad0f2eaae3cf10c589612f to your computer and use it in GitHub Desktop.
Save kadircs/f66fa8490dad0f2eaae3cf10c589612f to your computer and use it in GitHub Desktop.
integer(kind=c_int) function getstring(instr) bind(C,name='getstring')
use, intrinsic :: iso_c_binding
character(kind=c_char), dimension(*), intent(IN) :: instr
integer :: len
integer :: i
len=0
do
if (instr(len+1) == C_NULL_CHAR) exit
len = len + 1
end do
print *, 'In Fortran:'
print *, 'Got string: ', (instr(i),i=1,len)
getstring = len
end function getstring
gfortran -c acoustic.f90
gcc -c starsh.c
gcc -o bin acoustic.o starsh.o -lgfortran
./bin
#include <stdio.h>
int main(int argc, char **argv) {
int l;
char *name="IGRF";
l = getstring(name);
printf("In C: l = %d\n",l);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment