Pointers to Incomplete Types in Prototypes

Norman Diamond diamond at jit533.swstokyo.dec.com
Wed May 8 17:21:12 AEST 1991


In article <1991May8.043353.28983 at athena.mit.edu> scs at adam.mit.edu writes:
>In article <1991May7.043654.4795 at tkou02.enet.dec.com> diamond at jit533.enet@tkou02.enet.dec.com (Norman Diamond) writes:
>>The standard might have benefitted from distinguishing between tag
>>declarations and tag definitions, but it did not do so.
>Your bringing the issue out in the
>open like this makes me realize that there is no meaningful distinction.

Sorry, there is.

>A definition additionally implies space allocation, and perhaps content
>initialization.

For variables and functions, yes.

>However, a struct declaration, with or without a tag and with or
>without a struct-declaration-list, only enters identifier and
>type information into symbol tables.

And scope and storage class information.  If definitions were distinguished
from declarations, then the difference would be that declarations don't
enter type information into the table, and the scope issue might be cloudy.

>>In article <kre.673334440 at mundamutti.cs.mu.OZ.AU> kre at cs.mu.oz.au (Robert Elz) writes:
>>>It seems as if the compiler involved is treating the reference to bar in...
>	type foo(struct bar *arg);
>>>as a definition, as it hadn't seen a definition before.
>>>That is surely a bug.
>>
>>Yes.  No wonder I was dissatisfied with my previous posting on this
>>issue.  Something still seems wrong.
>
>There's nothing wrong here.  In the absence of any previous
>mention of a struct bar,
>	type foo(struct bar *arg);
>is as much of a struct "definition" as is
>	type foo(struct bar { ... } *arg);

No.  The standard says that the presence of "{ ... }" declares the a type.
No part of the standard says that "struct bar *" declares a new type.
"struct bar *" must be a reference.

Now:  The standard allows forward references for structure tags that
haven't been declared/[defined] yet.  It also allows references to structure
tags that are declared in outer scopes.  It does not explicitly say that a
single reference may take advantage of both of these features simultaneously.
But I think it must be allowed.  There is still no other meaning.

This answer runs counter to my previous beliefs and to everyone else's
posted beliefs, but it is the only one that is consistent with the standard.

>It can't be a "reference;" what (previous) struct bar could it be
>referring to?

A later one.  The standard allows forward references of structure tags.

>If mentioning
>	struct bar *
>didn't suffice to "define" a struct bar (assuming it wasn't already "defined"),

You betcha!  It doesn't!  RTFS.

>then the one-line translation unit
>	struct bar *bp;
>would have to give some kind of an error, much like the "warning:
>struct bar never defined" which lint issues under the -h option.

I think this might be true.  (Sorry, I'm not in the mood to look it up
at the moment.)

>The sole problem with
>	extern foo(struct bar *arg);
>at the top of some translation unit is that any call to foo later
>in that translation unit can't pass a compatible pointer-to-struct-bar.

It can complete the declaration of struct bar at a later time.
--
Norman Diamond       diamond at tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.



More information about the Comp.std.c mailing list