Re: Passing pointer to array of structs



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

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.
<snip>
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
syntactically

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
.



Relevant Pages

  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)
  • Re: Inconsistencies
    ... sizeofworked in someFunction as it does in main - so you could ... C allows you to declare what looks like an array parameter, ... really a pointer parameter. ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: variable length array
    ... it's legal to have one incomplete dimension. ... how would I declare u for 3 dimensions or more? ... legal to step through a 2D array as if it were a long 1D array. ... I showed the definition of the array and the pointer above. ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... an array of struct. ... You cannot assign a pointer to struct to a long* ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)