Previous: Naming Subroutines Up: Mixing Fortran and C Next: Passing Strings

Passing Numeric Arguments

The passing of numeric arguments and arrays between Fortran and C is fairly straightforward. The data type equivalences are listed in Table . A routine that receives data of one type must be passed the equivalent type of data if called from the other language. The bridge may of course change the data type between the caller and the routine if desired, but the subroutine interface that actually spans the language change (caller-bridge for Fortran to C, bridge-routine for C to Fortran) must pay attention to this table.

For numeric arguments, keep in mind that Fortran passes arguments by reference in all cases, while C normally passes input arguments by value. Such arguments must be converted in the bridge routine. A Fortran to C bridge would declare all arguments as pointers, then put asterisks (*) in front of the appropriate arguments to dereference them when calling the main routine. A C to Fortran bridge would declare the appropriate arguments as values (not pointers), and use an ampersand (&) in the call to the Fortran routine to convert them to pointers. This is shown in the examples in the previous section.

Be careful with the arguments, since not all C arguments are passed by value. If a pointer comes in, such as for an output variable or an array, then the pointer can normally be passed unchanged to the subroutine.

Two-dimensional (or higher) arrays can be a major problem, since Fortran treats things in column-major order, while C uses row-major order. This means the order of the subscripts is reversed. This can be handled either by copying the array and reshuffling it in the bridge (which can be quite inefficient), or by documenting the fact that the subscripts need to be reversed in the other language to call the routine properly.

rgd059@ipl.jpl.nasa.gov