Take the 2-minute tour ×

When trying to discover something in the code of some open source C projects, I often see this kind of typedef:

typedef struct _StructA StructA;
typedef struct _LinusTorvalds LinusTorvalds;

Why not directly define and use StructA, just StructA, instead of defining _StructA then "typedef" it?
What's the point of this "_" technique?

share|improve this question
Note that the underscore has nothing to do with this technique. typedef struct StructA StructA; would work as well. C uses 'tags' for naming structs, and those tags can have the same name as a typedef'd type. –  Matthew Jan 26 '12 at 7:51
I think a LinusTorvalds typedef is a bad example, as he doesn't like typedefs: git.kernel.org/?p=linux/kernel/git/torvalds/… :) –  Anders Waldenborg Jan 26 '12 at 8:10
add comment

4 Answers

up vote 7 down vote accepted

In C, a struct must be used as:

struct _StructA my_variable;

…i.e., you must type struct not only when you define the struct, but also every time you use it. The typedef eliminates the typing of struct when you use it (but not when you define it: there is no way to eliminate that). With a typedef then, declaring an instance of the struct becomes:

StructA my_variable;

Now, that doesn't completely explain the _StructA/StructA business in the defining of the struct/typedef, as you can do this:

typedef struct {
} StructA;

…here, we take an anonymous struct and immediately feed it to typedef. But using this method, you can't use the struct inside itself, as it doesn't (yet) have a name. For that, you need something like:

typedef struct _Foo {
    int data;
    struct _Foo *next;
} Foo;
share|improve this answer
"it eliminates the typing of ..." and then it looks like .. give an example to show the advantage. the 2nd snippet shows how to typedef an "anonymous" struct ... you should mention that somewhere (it conflicts a bit with the statement of 'no need to type'). –  akira Jan 26 '12 at 7:47
@akira: Clarified a bit. –  Thanatos Jan 26 '12 at 7:49
thanks mate, I got it. It's so simple and this question is so stupid ^^! C++ sometimes makes my C mad :) –  perfwill Jan 26 '12 at 7:53
Sorry I forgot to tick it –  perfwill Jan 27 '12 at 2:45
add comment

In C, when you define a struct type:

struct StructA {
    int foo;
    double bar;

the type's name is struct StructA, not just StructA. (C++ lets you refer to the type as just StructA; C++ is a different language.

If you insist on having a one-word name for it, you can use a typedef (which, we should note, creates a new name for an existing type, not a new type):

typedef struct StructA StructA;

Now you can refer to the type either as StructA or as struct StructA.

These can be combined into a single declaration:

typedef struct StructA {
    int foo;
    double bar;
} StructA;

Note that I used the same identifier for the struct tag and for the typedef. Since they're in different namespaces, this is perfectly valid.

On the other hand, my own preference is to omit the typedef altogether, and just refer to the type as struct StructA.

Incidentally, the names _StructA and _LinusTorvalds in your question are reserved to the implementation, and should not be used in your own code. In general, identifiers starting with an underscore should be avoided. (It's not uncommon to use such identifiers, but it's wrong, unless the code is part of the C implementation itself.)

To summarize: The typedef is necessary only if you want to have a one-word name for the type (which isn't really required) -- and if you do use a typedef, there's no real reason to use different identifiers for the typedef and the struct tag.

share|improve this answer
+1, but you forgot a typedef in your 3rd snipset. Also as a nitpick, the type is struct StructA in C++, too. C++ only allows you to omit the struct if there is no ambiguity, that is no other identifier with that name is declared in the same scope. Think of struct stat as an example. –  Jens Gustedt Jan 26 '12 at 8:57
@JensGustedt: Thanks for the nitpicks! –  Keith Thompson Jan 26 '12 at 9:08
this type of typedef is used extensively in the source code of Glib/Gtk and the ibus library :| –  perfwill Jan 27 '12 at 2:51
add comment

It provides the convenience of not using the struct keyword everytime you declare variables of the structure.

Without typedef:

struct _StructA obj;

With typedef:

StructA obj;
share|improve this answer
add comment

It could reduce the dependency to a certain data structure. E.g. if you ever want to use StructB (maybe containing more fields) instead of StructA, you need to change just one line, not hundreds.

share|improve this answer
add comment

Your Answer


