Asked By Doug Harrison [MVP]
03-Jun-08 11:36 AM

On Tue, 3 Jun 2008 05:36:22 -0700 (PDT), goodTweetieBird
Not sure what you meant to write there.
All the following are equivalent:
void f(int a[20][30]);
void f(int a[2][30]);
void f(int a[][30]);
void f(int (*a)[30]);
This is the *****only***** context in which array and pointer declaration
syntax is sort of interchangeable, and it's the reason the magnitude of the
first dimension doesn't matter - all four declare "a" to be a pointer to an
array of 30 ints. This has to be, because (ignoring C++ references) an
array passed to a function undergoes the array to pointer conversion, which
produces a pointer to its first element, so "a" has to be a pointer. For
your array int[20][30], this pointer has the type int(*)[30], or "pointer
to an array of 30 ints", and the number of these int[30] arrays is lost in
the conversion from array to pointer to first element. It's the same with
1D arrays; the pointer to the first element of an int[30] array is an int*,
and if all you have is the int*, there's no way to tell if it's a scalar or
an array, and no way to tell how big the latter is. No matter how you
declare the parameter "a", inside f, you index it as usual, e.g. a[0][0],
a[1][20], etc. As for the sort-of-interchanageable syntax mentioned
earlier, it's intended to ease the declaration of function parameters, but
it's the last declaration of "f" that expresses the underlying reality.
Don't think pointers and arrays are "the same"; they're not. Your array
int[20][30] is a solid block of ints and contains no pointers. All its
pointer-like characteristics stem from the array to pointer conversion.
--
Doug Harrison
Visual C++ MVP