Hi, all. I feel confuse about below struct definition:
typedef struct TAG
{
comments....
};
What my confusion is: is typedef extra??why we not just use
struct TAG
{
comments....
};
My question is: is there any difference between them?
You messed up the typedef. There are actually (at least) three
variants of what you're asking about.
1:
typedef struct foo {
/* member declarations */
} foo_t;
2:
typedef struct {
/* member declarations */
} foo_t;
3:
struct foo {
/* member declarations */
};
In these examples, "foo" is a struct tag, and "foo_t" is a typedef
(which is simply an alias for an existing type).
In example 1, we have two names for the same type, "struct foo" and
"foo_t". Note that struct tags are in their own namespace, so there's
no real need to use two different identifiers; you could have
typedef struct foo {
/* member declarations */
} foo;
Again, this gives you two names for the same thing, "struct foo" and
"foo".
In example 2, we declare an anonymous struct (with no tag) and then
create the name "foo_t" as an alias for it.
In example 3, there's no typedef, so we can only refer to the type as
"struct foo".
The form in example 1 is fairly common, but personally I prefer
example 3. There's really not much advantage in having an additional
alias for something that already has a perfectly good name. And you
need the "struct foo" form anyway if the structure needs to have a
member that's a pointer to the same type (e.g., for a linked list
node).