the name of the file you want to include. All text in file is prepended to each of the source files specified on the command line, before being passed to the compiler.

See “File handling” (page 120) for another example of a program that uses the $HP$ ALIAS directive. The HP Fortran Programmer's Reference fully describes the %VALand %REFbuilt-in functions, the +uppercase and +pre_includeoptions. The $HP$ ALIAS directive is discussed in “$HP$ ALIAS”.

Arrays

There are two differences between HP Fortran and C to consider when passing arrays from Fortran to C:

In HP Fortran, array subscripts start by default at 1, whereas in C they always start at 0

In HP Fortran, multi-dimensional arrays are laid out differently in memory than they are in C.

The difference in subscript-numbering does not result in any size discrepancies: an array of 10 elements in Fortran has 10 elements in C, too. But the subscripts in Fortran will be numbered 1 - 10, whereas in C they will be numbered 0 - 9. This difference should not require any change to the normal coding practice for C or for Fortran.

The difference in the way multi-dimensional arrays are laid out is well-known but more significant: Fortran lays out multi-dimensional arrays in column-major order, so that the leftmost dimension varies fastest; whereas C lays out multi-dimensional arrays in row-major order, so that the rightmost dimension varies fastest.

Figure 8-1 shows the Fortran and C declarations for a two-dimensional array of integers, each having the same number of rows and columns. The boxes under each array declaration represents the memory locations where each element of the array is stored. As shown, each language represents the six elements in a different order: the value stored at the first row and second column is not the same for Fortran as for C.

Memory layout of a two-dimensional array in Fortran and C

Figure 3 Memory layout of a two-dimensional array in Fortran and C

To compensate for this difference, the dimensions of the array in either the C or Fortran code should be declared in the reverse order of the other. For example, if the array is declared in Fortran as follows:

INTEGER, DIMENSION(3,6) :: my_array

then the array should be declared in C as follows:

int my_array[6][3];

You can change the array declaration in either language, whichever is more convenient. The important point is that, to be conformable, the dimensions must be in reverse order.

Below is an example for a three-dimensional array, the first being for a Fortran declaration.

REAL, DIMENSION(2,3,4) :: x

Below is the same declaration as declared in C.

int x[4][3][2];

116 Calling C routines from HP Fortran

Page 116
Image 116
HP UX Fortran Software manual Arrays, Memory layout of a two-dimensional array in Fortran and C, REAL, DIMENSION2,3,4, Int