Passing strings between C and Fortran routines is not recommended because there is no standard interface. However, note the following:
All C strings are passed by reference.
Fortran calls pass an additional argument for every argument with character type in the argument list. The extra argument gives the length of the string and is equivalent to a C long int passed by value. (This is implementation dependent.) The extra string-length arguments appear after the explicit arguments in the call.
A Fortran call with a character string argument is shown in the next example with its C equivalent:
Table 11-6 Passing a CHARACTER string
Fortran call: |
C equivalent: |
---|---|
CHARACTER*7 S INTEGER B(3) ... CALL CSTRNG( S, B(2) ) ... |
char s[7]; long b[3]; ... cstrng_( s, &b[1], 7L ); ... |
If the length of the string is not needed in the called routine, the extra arguments may be ignored. However, note that Fortran does not automatically terminate strings with the explicit null character that C expects. This must be added by the calling program.