Re: mutually referential (Pg 140 K&R2)
- From: Jeremy Yallop <jeremy@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 23 Apr 2005 18:35:38 +0000 (UTC)
Chris Torek wrote:
> Scopes can in fact apply here:
>
> struct s { long l; };
>
> void f(void) {
> struct s; /* this "vacuous" declaration clears the way */
>
> struct s { char c; } var;
> /* this is a new, different type also named s */
> ...
> }
In this example the vacuous declaration doesn't make any difference;
the inner "full" declaration is valid and declares a type distinct
from the outer declaration regardless. The vacuous declaration is
needed when you want a name in an inner scope to match a tag defined
in the (same) inner scope instead of a tag from the outer scope.
struct f { long l; };
void h(void) {
struct f; /* "vacuous" declaration */
struct g { struct f *p; };
struct f { char p };
}
If the vacuous declaration were omitted `p' would be a pointer to a
struct of the outer "f" type; the vacuous declaration hides the outer
struct declaration and introduces a new type that is completed later
on within the inner scope. Following the vacuous declaration all
references to "struct f" within the block refer to this inner type.
I can't think of a situation where this would actually be useful; it's
always more straightforward to give the inner struct a distinct name.
> In general, incomplete structure types are only usefully completed
> in the same scope (it is possible to complete them in an inner
> scope, but this has relatively little practical application).
I don't think there is a way to complete incomplete structure types in
an inner scope.
Jeremy.
.
- Follow-Ups:
- Re: mutually referential (Pg 140 K&R2)
- From: Chris Torek
- Re: mutually referential (Pg 140 K&R2)
- References:
- mutually referential (Pg 140 K&R2)
- From: G Patel
- Re: mutually referential (Pg 140 K&R2)
- From: Chris Torek
- mutually referential (Pg 140 K&R2)
- Prev by Date: How do I check if a file is empty in C??
- Next by Date: Re: How do I check if a file is empty in C??
- Previous by thread: Re: mutually referential (Pg 140 K&R2)
- Next by thread: Re: mutually referential (Pg 140 K&R2)
- Index(es):
Relevant Pages
|