next up previous 252
Next: Same Language - Different Compiler
Up: Representation of Data
Previous: Pointer Types


Arrays

Although the representation of a single numerical value is unlikely to cause a problem, the way that arrays of numbers are stored is different between different languages. One dimensional arrays are the least problem, but even then there are differences. In C, all arrays subscripts start at zero, and this cannot be changed. In FORTRAN, subscripts start at one by default, but this can be modified so that the lower bound of a dimension of an array can be any integer. What must be remembered is that the array element with the lowest subscript in a FORTRAN array will map on to the array element with a zero subscript when treated as a C array. This is not a serious problem as long as you remember it.

Multi-dimensional arrays are a well known problem since FORTRAN stores consecutive array elements in column-major order (this is specified in the FORTRAN standard) whereas other languages store them in row-major order. For example, in FORTRAN, the order of elements in a 2 x 2 array called A are A(1,1), A(2,1), A(1,2), A(2,2), whereas in C this would be A[0][0], A[0][1], A[1][0], A[1][1]. In practice this is rarely a serious problem as long as you remember to take account of the reversed order when writing a program. However, when coupled with the difference in default lower bounds (zero in C, one in FORTRAN) it is a fruitful source of bugs.

There are additional problems with FORTRAN character arrays. This is because C handles a one dimensional FORTRAN character array as a two dimensional array of type char, i.e. the FORTRAN statement:

CHARACTER * ( NCHAR ) NAMES(DIM)

is equivalent to the C statement:

character names[dim][nchar]



next up previous 252
Next: Same Language - Different Compiler
Up: Representation of Data
Previous: Pointer Types

CNF and F77 Mixed Language Programming -- FORTRAN and C
Starlink User Note 209
P.M. Allan
A.J. Chipperfield
R.F. Warren-Smith
19 January 2000
E-mail:ussc@star.rl.ac.uk