duns1222@w203zrz.zrz.tu-berlin.de (Martin Dunschen) (02/15/91)
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 <string.h> /* 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
dik@cwi.nl (Dik T. Winter) (02/16/91)
In article <160@mailgzrz.tu-berlin.de> duns1222@w203zrz.zrz.tu-berlin.de (Martin Dunschen) writes: > Hi all you high experienced C/FORTRAN programmers ! Do I qualify? > > But the last remaining thing is: > HOW TO PASS STRINGS ? > The only general answer is that the Fortran routines should not look at the parameters as strings, but as character array's (i.e. to determine the number of characters the routine should loop through the array searching for a terminator, which is not provided automagically in Fortran). If this does not satisfy you, more information is required: platform, fortran compiler. The answer can range from simple to impossible. (E.g. on Alliant systems Fortran passes the lengths of strings as parameter numbers -1, -2, etc.) -- dik t. winter, cwi, amsterdam, nederland dik@cwi.nl
ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (02/18/91)
In article <160@mailgzrz.tu-berlin.de>, duns1222@w203zrz.zrz.tu-berlin.de (Martin Dunschen) writes: > Hi all you high experienced C/FORTRAN programmers! > > We have a problem which is probably solved by some of you. > HOW TO PASS STRINGS ? The answer is "it depends". Which C are you using and which Fortran? For UNIX, AEGIS, and VMS, the answer is in (Fortran documentation, C documentation, C documentation) -- from memory. (Yes, the answer really is there in a complete UNIX documentation set. Or use f2c.) The usual technique in UNIX is to pass an extra int parameter for each character*(_) variable giving the length for that variable, after all the other parameters. -- Professional programming is paranoid programming