HP Fortran Programmer Guide (766160-001, March 2014)

In this example, it is assumed that the C routine has the array size information already coded into
it. If that is not the case, then the Fortran program must also pass the size as a separate argument,
and the C routine must be changed to accept a second argument.
For an example of a Fortran program that passes an array and its size as arguments to a C function,
see “Case sensitivity” (page 114). For an example of a Fortran program that passes character array
arguments to C, see “Passing a string” (page 118).
C strings
C strings differ from Fortran character variables in two important respects:
C expects strings to be null-terminated.
For each character variable or character constant that Fortran passes to a C routine, it also
passes a hidden length argument.
The following sections discuss these differences and explain how to code for them. The last section
includes an example program.
C null-terminated string
Unlike HP Fortran programs written in C expect strings to be null-terminated; that is, the last character
of a string must be the null character ('\0'). To pass a string from Fortran to C, you must do the
following:
Declare the character variable that is large enough to include the null character.
Explicitly assign the null character to the final element of the character array or use the
concatenation operator, as in the following example:
CALL csub ('a string'//CHAR(0))
If the Fortran program is going to use a string that has been passed back to it from C, then either
the C function or the Fortran subprogram should strip off the null character before Fortran tries to
use it. The example program in “Passing a string” (page 118)shows how to do this in C.
Fortran hidden length argument
For each CHARACTER*nargument passed to a Fortran subprogram, two items are actually passed
as arguments:
The address of the character argument in memory (that is, a pointer to the argument).
The argument's length in bytes. This is the “hidden” length argument that is available to the
subprogram from the stack.
To pass a string argument from Fortran to C, you must explicitly prepare the C function to receive
the string address argument and the hidden argument. The order of the address arguments in the
argument list will be the same in C as in Fortran. The hidden length arguments, however, will come
at the end of the list. If more than one string argument is passed, the length arguments will follow
the same order as the address arguments, but at the end of the C’s argument list.
Note that both C and Fortran both pass strings by reference. This means that, if Fortran passes
only string arguments to C, you need not use the %VAL and %REF built-in functions to indicate how
the arguments are to be passed. For information about these functions, see Argument-passing
conventions” (page 113).
Passing a string
The example program in this section illustrates how to pass a string—which, in Fortran, is a character
variable or constant—to a C function. It also illustrates how to process a C string so that it can be
manipulated in Fortran.
118 Calling C routines from HP Fortran