To aid the programmer in converting between the two forms of character strings, a number of C functions are provided in the CNF library. These handle all aspects of converting between the two types of string and provide options such as creating temporary strings, including the trailing blanks in the C version of a string and only copying a maximum number of characters. The process of converting from FORTRAN to C strings is known as ``importing'' and from C to FORTRAN as ``exporting''.
None of the functions are very complicated and some of them are just a tidier way of achieving what could be done with a few lines of C in the calling program. Consequently in a time critical application it may be appropriate to include the source of a CNF function in your code, rather than incur the overheads of a making a function call.
Full descriptions of the CNF functions are provided.
Here is an example of how to use them. This is the same as an example from the machine specific section of this document. The use of the F77 macros and the CNF functions have made the C code easier to write and completely portable to all Starlink systems.
PROGRAM STRING CHARACTER STR*20 CALL GETSTR( STR ) PRINT *,STR ENDC function:
#include "f77.h" F77_SUBROUTINE(getstr)( CHARACTER(fortchar) TRAIL(fortchar) ) { GENPTR_CHARACTER(fortchar) /* Generate pointer to fortchar */ char *string = "This is a string"; /* A string to be printed */ /* Copy the string to the function argument */ cnfExprt( string, fortchar, fortchar_length ); }Other examples in this document illustrate the use of CNF functions for importing strings and calling FORTRAN from C.
CNF and F77 Mixed Language Programming -- FORTRAN and C