Adam Lewis
Adam Lewis

Reputation: 7257

Proper typedef syntax in C

Short Question
Is there a proper or preferred way to to use typedefs of structs and enums in C?

Background
I have been working on a code base that has had several people / companies working on it and I have come across different implementations of typedefs. According to Wikipedia they all appear to have the same function. I guess I'm trying to understand if there are subtile differences or gotchas between them. The same usages and question applies to typedefing an enum.

// I tend to use this if I need to create an
// alias outside the defining module.

typedef struct obj obj_t;
struct obj
{
    UINT8 val;
};

// I started to use this format to alias objects
// within the same module (both private and
// public definitons).  This is also how 
// several colleagues use them.

typedef struct obj_t
{
    UINT8 val;
}obj_t;

// This is how I now typically format the aliases
// for both public and private definitions.

typedef struct
{
    UINT8 val;
}obj_t;

// I just ran into this one.  While makes sense 
// how it works, it wasn't inherently better or
// worse then the formats above. Note the different 
// container names.

typedef struct obj_t
{
    UINT8 val;
}obj;

Upvotes: 0

Views: 2584

Answers (5)

yoones
yoones

Reputation: 2474

typedef struct obj obj_t;
struct obj
{
    UINT8 val;
};

Writing the typedef before lets you use it inside the structure.

typedef struct obj_t
{
    UINT8 val;
}obj;

typedef struct obj_t
{
    UINT8 val;
}obj_t;

these two are for when you need to declare variables using either "struct obj_t" or "obj_t". I prefere using a t_ or _t for typedefs (more explicit), but it's really just a coding style matter, no technical chose to use one instead of the other.

typedef struct
{
    UINT8 val;
}obj_t;

Here you can only use the typedef since the structure has no name. Therefore you can't declare, inside your structure, a variable of this type.

Upvotes: 1

Bort
Bort

Reputation: 2491

Another subtile difference between your declarations is version 2 to 3. In 3, you use a typedef of an unnamed struct which is will give you an implicit cast error when you are going to cast it to let's say 2.

Upvotes: 1

Dayal rai
Dayal rai

Reputation: 6606

all given examples of typedefing a structure informs the same thing.Currently peoples are following all this either because of employer's style guide or for customer specific coding style.you have already given all possible struct type so i am adding ENUM only what you asked for

typedef enum season_t { SPRING, SUMMER, FALL, WINTER } Season;

for more standard answers you can look into below given link http://www.montefiore.ulg.ac.be/~piater/Cours/Coding-Style/

Upvotes: 0

spbnick
spbnick

Reputation: 5275

I wouldn't answer your question, but would instead suggest to consider what Linux kernel coding style has to say on the matter of typedefs. See "Chapter 5: Typedefs". Excerpt:

Please don't use things like "vps_t".

It's a mistake to use typedef for structures and pointers. When you see a

vps_t a;

in the source, what does it mean?

In contrast, if it says

struct virtual_container *a;

you can actually tell what "a" is.

Lots of people think that typedefs "help readability". Not so.

Read on for the suggested exceptions.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

They are all basically the same, in that they all define obj_t as a type alias for the structure.

The difference is when you define the name of the structure (e.g. struct obj ...) is that you can then also use struct obj myStructure; This is needed if you want to reference the structure inside it self (like when creating a linked list for example). If you do the typedef before the actual structure (like in your first example) then you can of course use the typedef'd name inside the structure as well.

Upvotes: 1

Related Questions