David Marsh said:
Is there any functional difference (or any other reason to prefer one
over the other) between these two methods:
typedef struct mystruct {
int a;
int b;
} mystruct;
struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;
I don't believe there's any real difference. Note that in both cases,
any reference to the type within its own definition (say, if a
mystruct contains a pointer to a mystruct) has to use the name 'struct
mystruct', since the typedef name doesn't exist yet.
You could even drop the tag, and just declare
typedef struct {
int a;
int b;
} mystruct;
since you never use the struct tag name anyway. But you'll need the
tag if you ever add a mystruct pointer as a member.
Using the same name for the struct tag and for the typedef is
perfectly legal, but may cause problems in some IDEs. If this induces
you to use different names, pick a consistent convention to avoid
confusion.
Richard Heathfield has argued for the second form on stylistic
grounds, on the basis that since two names are being declared ('struct
mystruct' and 'mystruct'), there should be two declarations. I'll let
him refute my blatant misrepresentation of what he actually said.
}
Others, myself included, have argued that in most cases typedefs for
strutures are superfluous. Your type already has a perfectly good
name, 'struct mystruct'; it doesn't need another. Using the 'struct'
keyword every time you refer to the type reminds the reader of the
relevant fact that the type is a structure type. If this fact is not
relevant, i.e., if you're creating an opaque type like FILE, then
using a typedef makes sense.
Plenty of very smart people disagree with me on this point, and
believe instead that having a one-word name for the type is
worthwhile.