Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Jun 2009 19:12:10 -0700
From:      Charlie Kester <corky1951@comcast.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: OT: C syntax question
Message-ID:  <20090630021210.GA72216@comcast.net>
In-Reply-To: <4.3.2.7.2.20090629204828.02ee3cb8@mail.pchotshots.com>
References:  <19017.24376.343126.389436@jerusalem.litteratus.org> <4.3.2.7.2.20090629204828.02ee3cb8@mail.pchotshots.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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".)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090630021210.GA72216>