In article <kZ********************@dls.net> Paul F. Dietz <di***@dls.net> asks:
Is the following legal C?
struct foo;
struct foo (*p)[10]; /* Pointer to array of 10 foo structures */
struct foo { int bar; int baz; };
main() { printf("%d\n", sizeof(*p)); }
(You should make it "int main()" and add a "return 0;" statement at
the end too, of course; and you need to #include <stdio.h>; but I
assume you have already done this. :-) )
I was going to say "yes"; then I checked the wording in the standard.
The first line creates the type "struct foo" as an incomplete type.
The second refers back to it and declares "p" as a pointer to "array
10 of that incomplete type." This is where the problem appears to
lie: the Standard says that an array can only have an object type
as its element type. An incomplete type is not an object type,
so "array 10 of <incomplete>" is not a valid array type.
Of course, there seems to be no particular reason to forbid pointers
to "array N of <incomplete>", especially since C *does* allow
pointers to incomplete types, including the type "pointer to array
? of T" for some object type T:
typedef some_object_type_here T;
extern T (*p2)[]; /* valid */
When the size is missing, as in this case, the array itself is an
incomplete type, so "p2" is a pointer to an incomplete type.
(Similarly, "void" is an incomplete type that can never be completed,
so "void *p3" makes p3 a pointer to an incomplete type.)
Still, this is what the standard says, however wrongheaded it might
be, so the second line appears to violate a constraint and require
a diagnostic. "gcc -ansi -pedantic" seems to agree with me, which
gives me additional confidence that I have not misinterpreted the
wording. :-)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.