Typedef struct vs just struct

Posted on 16th Feb 2014 by admin

Anyone know the rationale for using typedef for structs when a struct is itself a typedef? I have seen this in just about every book I have ever read, never with any explanation, yet I know from extensive personal experience that the end result is exactly the same (at least on all compilers from the last 10 years). Is it nothing more than an effort to save typing 'struct' on the variable declarations (and that not even needed in C++)? For instance, this all seems to be the same:

Code: typedef struct { int i; } bob; struct nob { int i; }; bob b; struct nob n;

I have heard people adamantly argue that everything should be typdef-ed as a way to make maintenance easier, but in my experience when you change a data type you still need to examine every single place it is used in the entire code base to make sure you aren't creating bugs with the type change, so I don't see the maintenance benefit. I find it a lot more useful to know what an object type is without needing to constantly look up what the heck the typedef really is (of course, for structs that doesn't help). Good IDEs will make that process less painful, but I have found my ability to maintain code is actually decreased with the extensive use of typedefs.

So, any arguments for the use of typedefs?

Other forums