5

typedef struct _Name {

} Name;

Or

typedef struct {

} Name;

...and why?

Comments
  • 5
    You need the first one if you have references to that type in the type itself - e.g. for prev/next pointers in linked lists. That's because the new type definition is only visible after the end of the statement.

    Also, you can give the same "Name" in both places.
  • 1
    @Fast-Nop Ah, I didn't know that. I learned that from the Windows API which does the underscore deal. Maybe left over from an old compiler? I know Windows supports backwards compatibility religiously
  • 1
    @AlgoRythm That's so that you can refer to the type either using the struct _Name or just Name method. Maybe if you have these self-referential declarations for linked lists, some people like to point out what exactly they are referring to, the struct or the resulting type. I guess it's a matter of taste.
  • 0
    One of the reasons I had found for actually naming structs is that you can't forward declare an anonymous struct(which is kind of obvious now I think about it) if for example you want to hide implementation details, even if you typedef it. Most of the time I just name things like
    typedef struct name_s {} name_t;
    Honestly there's not much reason not to name a struct.
Add Comment