tags:

views:

227

answers:

6

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?

A: 

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.

Bob Fincheimer
'most people don't like typing'. It's true. Any idea why? Your first method seems most normal and natural to me.
Brian Hooper
-1 That does not answer the question
Tomas
+2  A: 

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).

Justin Ethier
There was a typo error corrected by Amarghosh; the structure name and the typedef synonym name are different (forgot the underscore).Otherwise I get your point, even if I personnally prefer the second --shorter-- style.
Gui13
+1  A: 

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.

Darron
+3  A: 

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.

tristopia
Yeah I use it sometimes, but in this particular case I need the internal structure to be accessible from other parts of the program.
Gui13
You can do this encapsulation without the typedef. Simply putting a forward declaration, e.g. `struct whatever;`, in the header file will suffice.
Dan Moulding
A: 

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.

binW
+1  A: 

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.

Andrei Ciobanu
See also my answer @tristopia =)
Gui13