C array follies 1

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Sat Sep 7 16:37:05 AEST 1985


>     void f(x)
>         int x[2][2][2];
>     {
> ...
>     }
>     void main(){
>         int x[2][2][2];
> ...
>         f(x);
>     }
> ...
> I think that most systems will print "4 16 8" (or equivalent) ...

Yes, you are not actually passing an entire array to the function f() but
rather just a pointer to a 2x2 array of ints.  C lets you misdeclare this
	void f( int x[2][2][2] );	/* ANSI C assumed */
which is the real misfeature.  The "correct" declaration is technically
	void f( int (*x)[2][2] );
which also is supported by C (thankfully), or if you prefer,
	void f( int x[][2][2] );

Fortunately, old C did not have this misfeature for structs/unions,
so it was possible to add to C the ability to pass struct arguments
in addition to just pointers to them.  For reasons of backward
compatibility, it is unfortunately NOT possible to change C so that
	f( x );
actually passes the array and not just a pointer to the first element.

Maybe language "D" can fix all C's mistakes (ha).  Meanwhile we just
have to learn to cope with them.



More information about the Comp.lang.c mailing list