perfwill
perfwill

Reputation: 273

Just don't understand the point of these typedefs

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?

Upvotes: 1

Views: 407

Answers (4)

Keith Thompson
Keith Thompson

Reputation: 263267

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.

Upvotes: 3

Thanatos
Thanatos

Reputation: 44256

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;

Upvotes: 7

Alok Save
Alok Save

Reputation: 206526

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;

Upvotes: 1

Landei
Landei

Reputation: 54584

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.

Upvotes: 0

Related Questions