[net.lang.c] Array parameters

Paul Schauble <Schauble@MIT-MULTICS.ARPA> (01/08/85)

Pardon my lack of familiarity with modern C. I do hope someone will take
time to answer this:

From Joseph S.D. Yao <hadron!jsdy at SEISMO> commenting on the declaration

      int a[5][6];

as a function formal parameter.

> Oh, and i think you may have missed the earlier comment that, despite
> the syntactic sugar of declaring the argument as if it were an array,
> it actually i s a pointer, in compatibility with early versions of C
> that wouldn't let you pass anything longer than a word/longword as an
> argument.  The pointer's size is correctly returned by sizeof().

Current C compilers allow passing a structure as a parameter. That's a
real  structure, not a pointer to a struct as in the old style.

I presume then that one can pass a real array in the same fashion.

then if 

     function (a);
     int a[5][6]

says that the parameter is "really" a pointer, for compatability, what
does the declaration look like when the parameter is really an array?

Or where did I go wrong?

          Paul
.

Doug Gwyn (VLD/VMB) <gwyn@Brl-Vld.ARPA> (01/08/85)

No, arrays cannot be passed as actual arguments to a function
but only their addresses.
	function( a ) int a[5][6]; { ... }
has just one pointer datum passed to it, not 30 ints.  The difference
is significant; due to C's "pass-by-value" design, in the actual case
"function" can modify the caller's array while in the incorrect case
(entire array as argument) it could only modify its own local copy of
the array.

ndiamond@watdaisy.UUCP (Norman Diamond) (01/10/85)

In order to pass an array parameter by value (instead of passing a pointer
to it), the array has to be imbedded in a structure and the structure then
passed by value.

I suppose if one wants to pass a structure by reference, it could be made
an array of size 1.  (Of course, one could also use &, but obviously that
would be cheating -- if the use of & could be suggested for structure
references, then it could also be suggested for array references, and the
default could be by value consistently.)

-- Norman Diamond

UUCP:  {decvax|utzoo|ihnp4|allegra|clyde}!watmath!watdaisy!ndiamond
CSNET: ndiamond%watdaisy@waterloo.csnet
ARPA:  ndiamond%watdaisy%waterloo.csnet@csnet-relay.arpa

"Opinions are those of the keyboard, and do not reflect on me or higher-ups."

mat@hou4b.UUCP (Mark Terribile) (01/10/85)

> Current C compilers allow passing a structure as a parameter. That's a
> real  structure, not a pointer to a struct as in the old style.
> I presume then that one can pass a real array in the same fashion.

Nope.  Structures and arrays are fundamentally different sorts of aggregates
with very different properties.

>     function (a);
>     int a[5][6]

In this case, what you have is equivalent to

	int	(*a)[ 6 ];

-- a pointer to an array of six ints.  One of our local compilers
accepts all of these without complaint:
f( a )
int a[5][6];
{
	y( a );
}
g( a )
int a[5][];
{
	y( a );
}
h( a )
int a[][6];
{
	y( a );
}
i( a )
int (*a)[6];
{
	y( a );
}
-- 

	from Mole End			Mark Terribile
		(scrape .. dig )	hou4b!mat
    ,..      .,,       ,,,   ..,***_*.