Xref: utzoo comp.lang.c:36168 comp.lang.fortran:4769 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!opal!mailgzrz!duns1222 From: duns1222@w203zrz.zrz.tu-berlin.de (Martin Dunschen) Newsgroups: comp.lang.c,comp.lang.fortran Subject: mixing fortran and c Message-ID: <160@mailgzrz.tu-berlin.de> Date: 15 Feb 91 15:53:48 GMT Sender: news@mailgzrz.tu-berlin.de Organization: TUBerlin Lines: 79 Nntp-Posting-Host: w203zrz.zrz.tu-berlin.de Hi all you high experienced C/FORTRAN programmers ! We have a problem which is probably solved by some of you. We try to combine Fortran and C. All these things in Fortran, like COMMON Blocks, alternate returns, call by reference vs. call by value are no problems to us anymore. But the last remaining thing is: HOW TO PASS STRINGS ? It seems, that the Fortran subroutine does not know, how long are the given strings. Below are a few programs for testing purpose, two C-procedures and a fortran subroutine. The solution is perhaps something like giving the length of the strings in a way to the subroutine, or adding a special character, which tells the subroutine where the string stops. Thanks in advance, Martin. +---------------------------------------------------------------------------+ | e-mail : dunschen@zrzsp9.fb12.tu-berlin.de | | | | Martin Dunschen | | Institute of Naval Architecture | | Salzufer 17-19 | | D-1000 Berlin 10 | | Germany | | | +---------------------------------------------------------------------------+ CUT HERE --- CUT HERE --- CUT HERE --- CUT HERE --- CUT HERE --- CUT HERE --- #include /* main program for testing the passing of strings from c to fortran */ main () { strtst ("TESTING","CASE"); } /* procedure, called by main, parameters are the strings */ strtst (str1, str2) char* str1; char* str2; { char *stest1, *stest2; printf ("str1= %s\n str2= %s\n", str1, str2); printf ("length str1 %d\n", strlen( str1)); printf ("length str2 %d\n", strlen( str2)); getchar (); /* Try to add a sign to the strings, so that FORTRAN knows where to stop */ strcat (str1, "\n"); strcat (str2, "\n"); printf ("str1= %s ", str1); printf ("str2= %s ", str2); getchar (); /* call the fortran subroutine, passing pointers to strings */ strf_ (str1, str2); } c------ CUT HERE ------ subroutine strf (str1, str2) character *(*) str1, str2 print*, 'strf : ', str1, len(str1) print*, 'strf : ', str2, len(str2) end