How to pass subarrays?

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Fri May 3 07:03:24 AEST 1985


> #define S 20
> main()
> {
> 	int	big[S][S],
> 		AverageAtCenter;
> 
> 	GenerateArray( big );
> 	AverageAtCenter = LocalAverage( &big[ S/2 ][ S/2 ] );
> 	printf( "Average at center is %d\n", AverageAtCenter );
> }
> 
> LocalAverage( window )
> 	int	window[S][S],
> 		result;
> {
> 	result = window[-1][0] + window[0][1] +
> 		window[1][0] + window[0][-1];
> 	result /= 4;
> 	return result;
> }

(1) I don't think "result" is declared in the right place.
(2) You're passing an (int *) actual argument to the LocalAverage
function, but you have declared its formal parameter as an SxS array,
which is one level difference of dereferencing.  The formal parameter
is equivalent to
	int	window[][S];
or
	int	(*window)[S];
Because of the way code is generated, you can actually get away
with this type punning in this particular case.



More information about the Comp.lang.c mailing list