HP Fortran Programmer's Guide (B3908-90031; September 2011)

Calling C routines from HP Fortran
Arrays
Chapter 8194
printf("\nHere is the same array as accessed from C:\n\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++)
printf(“a[%d][%d] = %d\n”, i, j, a[i][j]);
}
Here are the command lines to compile, link, and execute the program, followed by the output from a
sample run:
$ cc -Aa -c get_array.c
$ f90 pass_array.f90 get_array.o
$ a.out
Here is how Fortran stores the array:
my_array(1,1) = 1
my_array(2,1) = 2
my_array(1,2) = 3
my_array(2,2) = 4
my_array(1,3) = 5
my_array(2,3) = 6
my_array(1,4) = 7
my_array(2,4) = 8
Here is the same array as accessed from C:
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4
a[2][0] = 5
a[2][1] = 6
a[3][0] = 7
a[3][1] = 8
In this example, it is assumed that the C routine has the array size information already coded into it. If that
is not the case, then the Fortran program must also pass the size as a separate argument, and the C routine
must be changed to accept a second argument.
For an example of a Fortran program that passes an array and its size as arguments to a C function, see “Case
sensitivity” on page 189. For an example of a Fortran program that passes character array arguments to C,
see “Passing a string” on page 196.