Converting FORTRAN to C

dgary at ecsvax.UUCP dgary at ecsvax.UUCP
Thu Jan 3 03:19:29 AEST 1985


David Herron's posting prompts me to clarify and correct my comments on
C's lack of the valuable ability to pass varying sized arrays of two or
more dimensions.  To recap briefly, FORTRAN and a few other languages
allow the passing of arrays to subroutines without the subroutine
"knowing" in advance the size of the array.  This is true in C only for
one-dimensional arrays, or multi-dimensional "arrays" set up as a chain
of arrays of pointers to arrays.

David repeats a common misconception when he says:

>                          ... Meaning
> that a two dimensioned array is actually a one dimensioned array of
> int *'s to one dimensioned arrays of ints.  This builds as you build
> deeper levels of indexing.

It is, of course, possible to create such an "array" in C, but an array
declared, say, "float array[20][30]" is NOT set up as an array of
pointers to an array of floats; it is all in contiguous storage, and C
does a routine subscript calculation to determine the offset (see K&R,
for example).  In a sense, this is a case of ambiguity in the C notation
array[][]; the compiler has to know the kind of animal it's dealing
with.  In practice, of course, this information is readily available
from the symbol table, and I can't think of too many cases where
the exact semantics would be important to the programmer.  Perhaps a
notation of array[i;j] would be useful for two-dimensional arrays of the
conventional (i.e., continguous storage) variety.   Anyway, David goes
on to quote me:

>> It is possible to pass variant-sized arrays in C by passing an array of
>> pointers to arrays, or by simulating a 2-dimensional array by means of a
>> one-dimensional one (handling the subscript computation with a #define
>> or a subroutine would be easiest here).  But I would like to see this
>> simple feature added to C; I doubt it would break any existing programs.
>
>Why is this important?  The compiler doesn't need to know how big arrays
>are ... ?
>
> David Herron;  ARPA-> "ukma!david"@ANL-MCS
> (Try the arpa address w/ and w/o the quotes, I have had much trouble with both.)
>
> UUCP          -:--:--:--:--:--:--:--:--:-          (follow one of these routes)
>
>{ucbvax,unmvax,boulder,research} ! {anlams,anl-mcs} -----\  vvvvvvvvvvv
>							  >-!ukma!david
>   {cbosgd!hasmed,mcvax!qtlon,vax135,mddc} ! qusavx -----/  ^^^^^^^^^^^

The compiler needs to know all but the last dimension size to compute
the offset.  By the way, I was incorrect when I said that C did not
specify row-major storage; it does, in fact, and David's description of
why is accurate and succinct.
-- 

D Gary Grady
Duke University Computation Center, Durham, NC  27706
(919) 684-4146
USENET:  {decvax,ihnp4,akgua,etc.}!mcnc!ecsvax!dgary



More information about the Comp.lang.c mailing list