Keith Thompson said:
A typedef for a struct type creates a new name (such as "foo") for a
type that already has a perfectly valid name (such as "struct foo").
Not necessarily; as long as struct type is not self-referential,
it can be typedef'ed without using a tag.
The "struct foo" name still has to be used inside the struct definition
if the type declares a pointer to the same type (unless you use a
forward declaration).
The cost of doing either of those is small. Besides, the most
important use case for typedef'ing a struct is to make a
(mostly) opaque type, in which case the header file will
forward-declare the struct type (without defining its contents)
along with the typedef, so the incremental cost of doing
a forward declaration is zero.
Using distinct identifiers for the tag and the typedef name are
different identifers, which is very common, can lead to confusion.
I can't really parse your sentence, but in any case it seems
mostly orthogonal to the question about whether it's a good
idea to use a typedef. Even though tags and typedef names
are in different name spaces, there can be confusion between
them, and they should be chosen with that possibility kept in
mind. It's easy to avoid confusion just by following some
sort of naming convention, such as (for example) appending
an '_s' to any struct tag that has an associated typedef.
(Or whatever other convention is suitable for the context,
that was meant just as an example.)
Using a typedef for a struct type has few benefits. The
drawbacks are not terribly serious if you do it right, but
in my opinion the benefits are minor enough that it's not
worth doing.
You haven't listed any of what you think are benefits.
I'm curious to know what those might be.
Of course if you think that having a one-word name for a type
is a significant benefit, then you're going to disagree with
this argument.
To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types. In most cases, as
far as client code codes, the less one knows about a data
type the better; similarly, the more a type is treated as
an abstract data type, the better. The main cost of using
raw struct types is not in the typing but in the thinking:
for most code, not only do I not want to know what sort of
thing is being handled, I want NOT to know, because knowing
clutters ones thinking. Using a typedef name doesn't
prevent me from knowing (since after all I can go look at
the typedef in whatever header file defines it), but it
makes it easy not to know if one is so inclined. (And
those not so inclined can always just go look...)