next up previous 252
Next: Handling Byte Strings (HDS Locators)
Up: Converting Between FORTRAN and C Strings
Previous: Converting Between FORTRAN and C Strings


The CNF Functions

FORTRAN stores CHARACTER strings as fixed-length strings filled with trailing blanks, whereas C stores them as a variable-length strings each terminated by the null character. Although C strings are of variable length, there must of course be enough space reserved to store the maximum length that the string ever reaches plus one more character for the trailing null.

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.

Example - Converting character arguments between FORTRAN and C.
FORTRAN program:
      PROGRAM STRING
      CHARACTER STR*20

      CALL GETSTR( STR )
      PRINT *,STR

      END
C 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.



next up previous 252
Next: Handling Byte Strings (HDS Locators)
Up: Converting Between FORTRAN and C Strings
Previous: Converting Between FORTRAN and C Strings

CNF and F77 Mixed Language Programming -- FORTRAN and C
Starlink User Note 209
P.M. Allan
A.J. Chipperfield
R.F. Warren-Smith
19 January 2000
E-mail:ussc@star.rl.ac.uk