How to pass subarrays?
Griff Bilbro
glb at ncsu.UUCP
Thu May 2 04:43:05 AEST 1985
<< Propitiation for the line eating trolls >>
I'm doing a lot of 2D programming lately. Conceptually everything
behaves like 2D arrays, so I want to access things with 2 indices.
That's easy enough, but I want to pass a window of an array to a sub-
routine so that it looks like the original array except that it's
offset. I want the subroutine to be able to use negative subscripts.
Here's an illustration:
#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;
}
At this point, I don't care about speed. I need the code to look
like the math. The code above works, but lint complains. Why does
lint complain? How can I make C do the indexing arithmetic the way
I want that lint likes? It amounts to saying w[i][j] is equivalent
to w[i*S+j]. Can't I declare a argument pointer to index like this?
More information about the Comp.lang.c
mailing list