Why 'struct foo *x' instead of 'foo *x'

Piercarlo Grandi pcg at aber-cs.UUCP
Sun May 28 21:58:24 AEST 1989


In article <9137 at csli.Stanford.EDU> jkl at csli.stanford.edu (John Kallen) writes:
    
    Does anybody know the reason why structs are defined as they are in C?
    I.e. why do I have to say "struct foo" instead of just the tag "foo"?

There are others I can think of, but most importantly is to make the grammar
more palatable to a recursive descent parser. In this way, by looking at the
first token in a "statement" you know whether it is a declaration (begins
with a a storage class, "struct", a fundamental type name, ... all of which
are reserved keywords).

Typedef'ed names require

In C++ it is difficult (without infinite lookahead, i.e. context sensitive
parsing, just like Algol68) to distinguish declarations from imperative
statements, especially as they now may appear at any point in a block,
and especially as the '=' in an initializer can now be omitted again (even
if in a different way from early C, but with much the same ambiguities).

In certain contexts, some compilers require you to insert a storage class
specifier to help making the context less ambiguous. Indeed, I am now of the
opinion that it may help even the human reader to start any and every
declaration in C++ with the storage class, even if it is redundant (like
auto for locals and extern for globals).
-- 
Piercarlo "Peter" Grandi           | ARPA: pcg%cs.aber.ac.uk at nsfnet-relay.ac.uk
Dept of CS, UCW Aberystwyth        | UUCP: ...!mcvax!ukc!aber-cs!pcg
Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg at cs.aber.ac.uk



More information about the Comp.lang.c mailing list