Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "typedef struct foo foo;" considered harmful?

Tags:

c

coding-style

In typedef and struct namespaces in C vs C++, one of the comments seems to imply that exposing some struct foo is preferable to using typedef along the lines of...

typedef struct foo foo;

...and then using foo instead of struct foo throughout the API.

Are there any downsides to the latter variant?

like image 353
Philip Avatar asked Apr 20 '12 08:04

Philip


1 Answers

On whether or not to typedef structure types:

Here is some opinions around (all against typedefing structures):

From OpenBSD style guide:

"Avoid using typedefs for structure types. This makes it impossible for applications to use pointers to such a structure opaquely, which is both possible and beneficial when using an ordinary struct tag."

From Linux kernel coding style:

"It's a mistake to use typedef for structures and pointers."

From Expert C Programming by Peter Van der Linden:

"Don't bother with typedefs for structs. All they do is save you writing the word "struct", which is a clue that you probably shouldn't be hiding anyway."

like image 188
ouah Avatar answered Oct 02 '22 16:10

ouah