Passing strings between C and Fortran routines is not encouraged. However, a Fortran character-string-valued function is equivalent to a C function with two additional first arguments—data address and string length. The general pattern for the Fortran function and its corresponding C function is:
Fortran function |
C function |
|
---|---|---|
CHARACTER*n FUNCTION C(a1, ..., an) |
|
Here is an example:
Table 11–15 A Function Returning a CHARACTER String
Fortran calls C |
C calls Fortran |
||
---|---|---|---|
|
|
In this example, the C function and calling C routine must accommodate two initial extra arguments (a pointer to the result string and the length of the string) and one additional argument at the end of the list (length of character argument). Note that in the Fortran routine called from C, it is necessary to explicitly add a final null character. Fortran strings are not null-terminated by default.