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

Tom Karzes karzes at mfci.UUCP
Mon May 29 05:06:03 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"?

I think one reason is that it allows pointers to foo before foo itself is
actually defined.  For example:

    struct foo {
        struct bar *pb;
    };

    struct bar {
        struct foo *pf;
    };

If you tried to say "bar *pb;" before bar was defined, you'd get an error.
(Note that although C is willing to assume the same machine representation
for all structure pointers, this is not true for arbitrary types (e.g.,
char *, int *, etc.)



More information about the Comp.lang.c mailing list