"Jonathan Underwood" <> wrote:
> I have a 2 dimensional array allocated as a contiguous chunk of
> memory, as described in Question 6.16 in the FAQ (i.e. in the manner
> of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
> I have a function declared as func(..., array[],...) and I wish to
> pass my 2D array to that function. As I understand it, this should be
> allowable since the 2D array is contiguous in memory, and if i
> therefore call it as func(..., my2darray[0], ...) the data types
> should match (double * in both cases). Is this the correct way to do
> this?
Yes, this is fine with the definition as in FAQ 6.16:
int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
However, if it were a normal array with a definition like:
double my2darray[4][7];
The expression my2darray[0] has type 'double[7]' or 'array of 7 double'.
It is undefined behaviour to attempt to access beyond the 7th element
of my2darray[0], that is beyond my2darray[0][6], so you're not supposed
to access the whole of my2darray through my2darray[0].
This could generate problems on a really good bounds-checking
implementation of C. Most implementations won't catch it though.
--
Simon.