Sun Studio 12: Fortran Programming Guide

11.1.7 Array Indexing and Order

Array indexing and order differ between Fortran and C.

11.1.7.1 Array Indexing

C arrays always start at zero, but by default Fortran arrays start at 1. There are two usual ways of approaching indexing.


      INTEGER B(0:2)

This way, the Fortran element B(1) is equivalent to the C element b[1].

11.1.7.2 Array Order

Fortran arrays are stored in column-major order: A(3,2)


A(1,1)  A(2,1)  A(3,1)  A(1,2)  A(2,2)  A(3,2)

C arrays are stored in row-major order: A[3][2]


A[0][0] A[0][1] A[1][0] A[1][1] A[2][0] A[2][1]

This does not present a problem for one-dimensional arrays. However, with multi-dimensional arrays, be aware of how subscripts appear and are used in all references and declarations—some adjustments might be necessary.

For example, it may be confusing to do part of a matrix manipulation in C and the rest in Fortran. It might be preferable to pass an entire array to a routine in the other language and perform all the matrix manipulation in that routine to avoid doing part in C and part in Fortran.