OT: C syntax question

Charlie Kester corky1951 at comcast.net
Tue Jun 30 02:12:14 UTC 2009


On Mon 29 Jun 2009 at 17:51:37 PDT Brad Mettee wrote:
>
>I believe since you are declaring the array as having a fixed number 
>of elements, you must declare the function to take it the same way, 
>like this:
>
>  extern int plate_shift(struct CONTINENT *[10][10],int,float);
>
>Without the 10,10 size definition, the plate_shift function would have
>no idea how big the array of pointers actually is.


Close.

If you pass a two-dimensional array as an argument, you must specify at
least the number of elements in the "minor" dimension, so the compiler
will know how to compute, for example, the offset of array[3][0] versus
array[6][0].

So this works:

    extern int plate_shift(struct CONTINENT *[][10], int, float);

But the following doesn't work, because it doesn't tell the compiler how
to compute the offsets:

    extern int plate_shift(struct CONTINENT *[10][], int, float); 

Of course, it doesn't hurt to specify the number of elements in
both dimensions.  But it would be a less general solution,  With
only the minor dimension specified, plate_shift can take pointers
to arrays of 20, 30, 40, 100 or 200 CONTINENT structures.  Any
multiple of 10 will do.  (But then you need to tell plate_shift when
it's reached the last set of pointers, or it might run off the end of
the array.  The usual way to handle this is to use all NULLs in the last
row, as a kind of "sentinel".)


More information about the freebsd-questions mailing list