For most data types arrays are handled simply, using pointers as already demonstrated. However, for arrays of some types the data in the arrays must be converted back and forth between C and FORTRAN representations. Macros and functions are provided to facilitate the conversions.
Very often, the actual size of the FORTRAN array required will not be known until runtime so space for it must be allocated dynamically in a similar way to dynamic character strings.
Macros DECLARE_type_ARRAY_DYN and F77_CREATE_type_ARRAY are defined to do this. They are
designed for 1-dimensional arrays, having just the name and the number of elements as parameters,
but for Unix systems, at least, will work for multi-dimensional arrays.
For most types on all current systems, the CREATE_ARRAY macros will not actually allocate
space as no conversion of data is necessary, but they are provided for contingency and
completeness.
There are two versions of the macros for creating dynamic CHARACTER and LOGICAL arrays:
F77_CREATE_CHARACTER_ARRAY will create a 1-dimensional array with the given number of elements,
and F77_CREATE_CHARACTER_ARRAY_M will create an array whose size is defined by an integer
specifying the number of dimensions and an array of integers specifying each dimension. Similarly
F77_CREATE_LOGICAL_ARRAY and F77_CREATE_LOGICAL_ARRAY_M
Consider the following example of a C program which calls a FORTRAN subroutine which returns a
CHARACTER array produced by setting to blank every non-blank element of a given array for which the
corresponding element of a given LOGICAL array is TRUE. A LOGICAL output array is produced with
TRUE in the element corresponding with each element of the CHARACTER array which has been reset,
and FALSE elsewhere.
As an example of how to write a C function to be called from FORTRAN with array arguments, the above subroutine could be re-written in C as follows:
pointer to charIn C, arrays of character strings are often held as arrays of pointer to char. This allows strings of
varying length and not necessarily in contiguous memory. CNF functions cnfImprtap and cnfExprtap
can be used to import/export arrays of pointer to char from/to FORTRAN CHARACTER arrays. The
following example shows how to do this. The FORTRAN subroutine, PRARR, prints the given
CHARACTER array and returns it set to blank strings. The C program prints the strings before and after
the call to PRARR.
An array of pointers would need to be converted back and forth between the C and FORTRAN
representations to cope with the possibility that the length of a C pointer is not the same as the length
of a FORTRAN INTEGER. This can be done by declaring a suitably-sized FORTRAN array and
converting each element using either cnfCptr or cnfFptr, according to the direction of
conversion.
For example, to call a FORTRAN subroutine which returns an array of three pointers to real, the C code would need to be something like:
See also The IMPORT and EXPORT macros (Section 10).