Up | Previous | Next | Title Page | Contents

7.2 String Conversion API

sc2for—C null-terminated string to an output FORTRAN string

sc2for(c_string, max_length, for_string, argptr, nargs, argno, strno);
This routine converts a standard C null-terminated string to an output FORTRAN string. It is used to send strings back to a FORTRAN caller.

Arguments:

7.2.2 sc2for_array—C null-terminated array of strings to FORTRAN string array

sc2for_array(c_string,len,nelements,for_string,max_length,argptr,nargs, argno,strno);
This routine converts a standard C null-terminated array of strings into an output FORTRAN string array. The C string array must be a two-dimensional array of characters, not} an array of pointers to strings. The FORTRAN string should be declared as a single-dimensional array of CHARACTER*n in the calling routine.

Arguments:

7.2.3 sfor2c—FORTRAN input string to a standard C null-terminated string

sfor2c(c_string, len, for_string, argptr, nargs, argno, strno);
This routine converts FORTRAN input string to a standard C null-terminated string. It is used to receive string parameters from a FORTRAN caller.

Arguments:

7.2.4 sfor2c_array—FORTRAN string array to C null-terminated array of strings

sfor2c_array(c_string, max_length, nelements, for_string, argptr, nargs, argno, strno);
This routine converts a FORTRAN string array to a standard C null-terminated array of strings. The returned C string array is a two-dimensional array of characters, not} an array of pointers to strings. The FORTRAN string should be declared as a single-dimensional array of CHARACTER*n in the calling routine.
This routine is somewhat unusual in that it actually allocates the memory for the C string for you. You pass in the address of a character pointer, not the address of a buffer for the characters. sfor2c_array calls malloc()}to allocate the required memory, and returns the address of that memory in the pointer. It is your responsibility to call free()to free up that memory when you are done with it.

Arguments:

This routine actually allocates the memory for the C string for you. C_STRING is the address of a character pointer, not the address of a buffer for the characters. sfor2c_array} calls sub malloc() to allocate the required memory, and returns the address of that memory in C_STRING. It is your responsibility to call free() to free up that memory when you are done with it.

The inner dimension of the array is returned via the MAX_LENGTH parameter. Since you don't know this size at compile time, you can't access the strings like a normal two-dimensional array. It is easy enough to do your own addressing, however. For example:

char *array;
int maxlen=0;
...
sfor2c_array(&array, &maxlen, ...);
...
process_string(array+(i*maxlen));       /* to get at the i'th string */

On output, MAX_LENGTH returns the size of the inner dimension of the C string array that was allocated by sfor2c_array}. To access the i'th string in the array, simply add i*maxlen to the returned array pointer.

7.2.5 sfor2len—Length of a FORTRAN string

length = sfor2len(for_string, argptr, nargs, argno, strno);
This routine returns the length of a FORTRAN string. It does not get a pointer to the characters, nor does it convert them to a C string. It is most useful to get the length of a string in order to allocate a buffer for it before calling sfor2c. Note that the length returned is the “n” in the CHARACTER*n declaration, not the number of characters currently in the string.

Arguments:

7.2.6 sfor2ptr—Pointer to actual characters in FORTRAN string

ptr = sfor2ptr(for_string);
This routine returns a pointer to the actual characters in an input FORTRAN string. It does not get the FORTRAN string length, nor does it copy the string to an output C string. It merely returns a pointer to the characters. No guarantee is made that any of the characters are valid, since that depends on the FORTRAN string length. You can be sure that there will not be a null terminator. Some machines may have one, but you may not depend on a null terminator being there.
This routine should be used sparingly; use sfor2c for most FORTRAN string conversion. sfor2ptr is mainly intended for use in scanning a variable-length argument list to find the end-of-list marker. It is used extensively inside the RTL for this purpose. It should only rarely if ever be used in application code.
Note that only the FOR_STRING standard argument is required. This is because the FORTRAN string length is ignored. All the other parameters are used to find the length.

Arguments:


Up | Previous | Next | Title Page | Contents