When should one of the following statements be used over the other?
typedef struct Foo {
int a;
} Bar;
and
typedef stru
A lot of other people are focusing on the self referential aspect of this, but another reason to avoid doing this is that due to the lack of namespaces in C. In some circles it is standard practice to not typedef
structs to avoid struct
qualifier and instead refer to structs with the full specifier (eg void foo(struct Foo* foo_ptr)
). So if you wanted to maintain such a style, you wouldn't have the option to abuse anonymous structs, so this:
typedef struct {
int a;
} Bar;
Bar bar1 = {5};
should always instead be
struct Bar{
int a;
};
struct Bar bar1 = {5};
otherwise you couldn't even compile bar1's instantiation with out typedef
ing away the struct
qualifier