HP C Programmer's Guide (92434-90009)

156 Chapter5
Programming for Portability
Calling Other Languages
The C main program is compiled using cc -Aa x.c xx.o.
Another area for potential problems is passing arrays to FORTRAN subprograms. An
important difference between FORTRAN and C is that FORTRAN stores arrays in
column-major order whereas C stores them in row-major order (like Pascal).
For example, the following shows sample C code:
int i,j;
int array[10][20];
for (i=0; i<10; i++) {
for (j=0; j<20; j++) /* Here the 2nd dimension
varies most rapidly */
array [i][j]=0;
}
Here is similar code for FORTRAN:
integer array (10,20)
do J=1,20
do I=1,10 !Here the first dimension varies most rapidly
array(I,J)=0
end do
end do
Therefore, when passing arrays from FORTRAN to C, a C procedure should vary the first
array index the fastest. This is shown in the following example in which a FORTRAN
program calls a C procedure:
integer array (10,20)
do j=1,20
do i=1,10
array(i,j)=0
end do
end do
call cproc (array)
.
.
.
cproc (array)
int array [][];
for (j=1; j<20; j++) {
for (i=1; i<20; i++) /* Note that this is the reverse from
how you would normally access the
array in C as shown above */
array [i][j]= ...
}
.
.
.
There are other considerations as well when passing arrays to FORTRAN subprograms.