HP Fortran Programmer's Guide (September 2007)
Calling C routines from HP Fortran
Arrays
Chapter 8 187
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];
Example 8-5 pass_array.f90
PROGRAM main
! This program initializes a multi-dimensional array,
! displays its contents, then passes it to a C function,
! which displays its contents. The C function has the
! following declaration prototype:
!
! void get_array(int a[4][2]);
!
! Note that the dimensions are declared in reverse order
! in C from the way they are declared in Fortran.
INTEGER, DIMENSION(2,4) :: my_array = &
RESHAPE(SOURCE = (/1,2,3,4,5,6,7,8/), SHAPE = (/2,4/))
PRINT *, 'Here is how Fortran stores the array:'
DO i = 1, 4
DO j = 1, 2
PRINT 10, j, i, my_array(j,i)
END DO
END DO
! There’s no need to use the %VAL or %REF built-in functions
! because both C and Fortran pass arrays by reference.
CALL get_array(my_array)
10 FORMAT(‘my_array(', I1, ',', I1, ') =', I2)
END PROGRAM main
Below is the source file for a HP Fortran program that calls a C function, passing a
two-dimensional array of integers.
The following is the source file for the C function.