Why using a typedef *after* struct definition?

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

Both this style:

struct _something {    ... }; typedef struct _something someting; 

and that style:

typedef struct _something {   ... } something; 

are correct typedef declarations in C.
Note that the presence of the structure declaration in the header file is made on purpose: I need to have access to the inner components of the structure somewhere else.

One drawback of the first declaration is that when you use any IDE, the automatic "jump to declaration" often directs you to the typedef struct _something someting; instead of giving you directly the real structure definition.

In the second method, you get directly to the structure definition.

Is there a reason why one would use the first method?
The code I'm working on is full with these...
Is it simply a bad/good habit from the maintainers?

回答1:

This discussion thread gives a good overview of the topic, and highlights an important reason to use the first style:

This style separates type definition (which is not what typedef does) from typename synonym creation (which is what typedef does), and retains a strong correspondence between type name and type synonym without the disadvantage of using the same name for both (which can confuse some debuggers, and in any case is a pain for grep).



回答2:

There's one clear advantage of separating the typedef and the struct declaration and that advantage is clear if you separate both in different files. It allows for data encapsulation.

You declare in the header file your

typedef struct whatever typename; 

This is a declaration of a type of struct whatever without knowing how it is implemented. You can declare now the functions that are part of the interface, without revealing anything from the interna of the structure. Passing a pointer to that typename is all that is needed.

In the implementation file of your "class" (I put that in quotes as we are talking here purely in a C context), you do this:

#include "header.h"  struct whatever {   ...   ...   /* can even contain references to `struct whatever` or even `typename` */ }; 

This encapsulation has the advantage that you can change the internal structure without needing to recompile the rest of the app. Can come handy if you work with dynamic libraries.



回答3:

There isn't a technical reason to choose between the two ways of typedef'ing a structure.

Some people probably use the first method because they learned typedef's and structure declarations separately and they're just not combining them. Or perhaps their IDE's prefer the other form.

Or perhaps there was a coding style selected by whim before powerful IDE's became available.



回答4:

If you separate the two things (typedef from the struct declaration), you will be able, if needed, to have opaque pointers.

Think to the opaque pointer as the primitive way of encapsulating information inside a certain structure, without letting the user to access the inside information.

Read also @tristopia's answer.



回答5:

when you define a struct:

struct something {    ... }; 

You have created a new type called 'struct something'.

You could use this accross your application like:

struct something myvar; 

But most people don't like typing struct (especially with lots of pointers):

struct something *test = (struct something*)malloc(sizeof(struct something)) 

So instead you typedef it (in either way you described):

struct something {    ... }; typedef struct _something someting; 

or

typedef struct _something {   ... } something; 

That way when you use the struct you can do this:

something *test = (something*)malloc(sizeof(something)) 

As far as which way you define it, it doesn't matter.

Some people like to put all their typedefs in one header file so they can typedef the struct, a pointer to the struct, etc.



回答6:

Some times you may want to create multiple synonyms for single type. In this case you are better off using first method as this gives you all synonyms i.e typedefs in one place.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!