Re: mutually referential (Pg 140 K&R2)



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



Relevant Pages

  • Re: Why connot declare a static member of STRUCT or UNION?
    ... scope, or function prototype scope. ... but a member of that object to have internal linkage (or vice ... the struct definition is visible. ... dynamically allocated instance is free'd, ...
    (comp.lang.c)
  • Re: mutually referential (Pg 140 K&R2)
    ... >In this example the vacuous declaration doesn't make any difference; ... >needed when you want a name in an inner scope to match a tag defined ... >in the inner scope instead of a tag from the outer scope. ... the vacuous declarations of struct ...
    (comp.lang.c)
  • Re: Why connot declare a static member of STRUCT or UNION?
    ... scope, or function prototype scope. ... A single struct type, once ... explain how this is a unique natural extension of that meaning? ... static int struct_foo_y; ...
    (comp.lang.c)
  • Re: Problems dereferensing my C structures (custom types)
    ... implementation, at file scope. ... This declaration probably is at file ... _tbl_certificate is not a legal name for that struct. ... (Identifiers beginning with __ or _ and a capital letter are reserved ...
    (comp.lang.c)