INF: Passing Two-Dimensional Arrays Between C and FORTRAN

ID Number: Q43000

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, when passing two-dimensional arrays from FORTRAN to C and vice

versa, it is important to note that the indexing conventions for the two

languages are different.

C arrays are indexed row followed by column, that is, the second index

varies the quickest. However, FORTRAN is indexed oppositely: in

FORTRAN, two-dimensional arrays are indexed with the first index

varying the quickest. Thus, passing two-dimensioned arrays requires

modification to either the C code or the FORTRAN code.

For more information regarding passing arrays between C and FORTRAN,

see page 127, Section 9.1.2, "Array Declaration and Indexing,"

in the "Microsoft Mixed-Language Programming Guide for the MS-DOS

Operating System" or page 311 of "Advanced Programming Techniques".

More Information:

The following code samples define an array in a common block in

FORTRAN, then use C to print the arrays to the screen:

Sample FORTRAN Code

-------------------

/* Compile options needed:

*/

c program mix_for.for

c

c

c this is to be used with mix_c.c......

c

subroutine test ()

common/cblock/array(0:8,0:1)

integer*4 i,j

do 20 i = 0,8,1

do 30 j = 0,1,1

array(i,j) = i

write(6,*)'the value of (',i,':',j,') is ',array(i,j)

30 continue

20 continue

end

Sample C Code

-------------

/* Compile options needed: none

*/

/* program mix_c.c

this program is to be used with mix_for.for......

*/

#include <stdio.h>

struct common_blk

{

float array[2][9]; /* note that the subscripts are reversed */

};

extern void fortran test( void );

extern struct common_blk fortran cblock;

void main()

{

int i,j;

test();

for(i=0;i<=8;i++)

for(j=0;j<=1;j++)

printf("The value of array[%d][%d] is %f",

i, j, cblock.array[j][i] );

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00