Re: Passing pointer to array of structs

On Wed, 26 Mar 2008 22:33:54 -0700, Barry Schwarz <schwarzb@xxxxxxxx>

Actually, the parameter for f1 (as it appears in the function
definition) is a pointer to a struct and the argument that is passed
(as it appears in the calling statement) looks like an array. However,
there is a clause in the language definition, sometimes called THE
RULE, that says: with three exceptions (which you should have no
trouble finding on google), an expression with array type is
automatically converted the address of the first element of the array
with type pointer to element type.
void f1(BDINFO* pbdArr)

One effect of this rule is that you could declare the function as
void f1(BDINFO pbdArr[]) or
void f1(BDINFO pbdArr[235])
since all three function signatures are **exactly** equivalent

I don't think it helps to view the equivalence here in terms of the array
to pointer conversion. This is the only context in which array and pointer
/declaration/ syntax are equivalent, and the rule you mentioned concerns
the use of arrays in /expressions/. BTW, the five exceptions to the rule I
can think of for an array "a" are:

1. &a
2. sizeof(a)
3. char x[] = "abc";
4. typeid(a)
5. Binding to an array reference

The OP should note that changing "char x[]" in (3) to "char* x" totally
changes the meaning, as does supplying an array length != 4. Another
classic example of the different meaning of the declaration syntax is using
"char x[10]" to define a global in one translation unit and "extern char*
x" to declare it in another, or vice versa. It will typically compile and
link fine but blow up at runtime due to it being treated in expressions as
sort of a bogus pointer/array union.

Doug Harrison
Visual C++ MVP