Should I convert FORTRAN code to C?

Chris Torek chris at mimsy.UUCP
Tue Jun 28 20:31:29 AEST 1988


>In article <738 at naucse.UUCP> rrr at naucse.UUCP (Bob Rose) suggests that
this C code is is legal:
>> 	double a[2][5]        /* <-- note [2][5] not [5][2] */
>> 	....foo(a)....
>> 	void foo(double a[10]) { ... }

In article <1190 at mcgill-vision.UUCP> mouse at mcgill-vision.UUCP (der Mouse)
gets the right answer, and the right reason:
>No, I'm afraid not.  foo() is expecting an array of ten doubles.
>Instead, you're passing it ... Actually, a pointer to an array of
>five doubles.)

but the wrong explanation:
>What's the difference?  To put it briefly, I find no guarantee that
>sizeof(double[5]) == 5*sizeof(double).

There is indeed such a guarantee.  The one that is missing, which makes
this particular example fail, is that pointers to arrays of objects need
not at all resemble pointers to the objects themselves.

Please remember that C is a strongly typed language (no matter what
else you have been told).  Mixing types is often legal, and the
cross-checking required by the language is extremly weak, but the
type system itself is quite strong.  The difference is like that
between an extensive law system and extensive enforcement:  If the
system is there, any missing enforcement can be provided later, at
need, without much hassle.

The whole example may be corrected quite simply:

	... foo(&a[0][0]); /* or foo(a[0]) */ ...

	void foo(double a[10]) /* or void foo(double *a) */ { ... }
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list