9

I have seen source codes always having a typedef for a structure and using the same everywhere instead of using the structure name as "struct sname" etc directly?

What is the reason behind this? Are there any advantages in doing this?

N 1.1
  • 12,418
  • 6
  • 43
  • 61
Jay
  • 24,173
  • 25
  • 93
  • 141
  • In his book *Expert C Programming*, Peter van der Linden makes the case that typedefs for structs are useless and you should not bother. I like this. All a typedef does is it saves you writing `struct` in a few places like declaratations and prototypes. It's not worth the obfuscation. I want to immediately know when I have to use `->` or `.`. Typedefs make this harder. – Jens Aug 20 '17 at 14:19

7 Answers7

10

Its easier to read Box b; than struct boxtype b;

typedef struct _entry{
   char *name;
   int id;
} Entry, *EntryP;

Advantage:
In the above typedef, both Entry & EntryP are defined apart from struct _entry.
So, EntryP firstentry can be used in place of struct _entry *firstentry and is a little simpler to parse in mind.

Note: Its not like structure names should be typedefined, but evidently its easier to read. Also, use of Entry * vs EntryP is totally user-dependent.

N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • 3
    I generally argue against typedefs like `EntryP`. C already has a perfectly good way of declaring the pointer you want: `Entry *firstentry`. – Dale Hagglund Apr 11 '10 at 21:40
  • 1
    @Hagglund: There is nothing to argue about :). Its totally subjective, one may like `EntryP` while other may like `Entry *`. – N 1.1 Apr 11 '10 at 22:35
  • 1
    I agree it's subjective but I got to think its not recommended as it abstracts away the fact that `firstentry` is of type `Entry *` for no good reason. Although the 'P' in `EntryP` is meant to emphasize the 'pointer to', it is still ambiguous whereas `Entry *` is not. In any case, I believe your line `struct entry *firstentry` should be `struct _entry *firstentry`, note the underscore – SiegeX May 12 '10 at 00:08
4

It is an odd quirk in the C language, structure tag names are stored in a different namespace (symbol table). The C++ language got rid of this behavior. The typedef creates an alias for the structure type in the global namespace. So you don't have to type "struct" before the structure name.

Not sure what Ritchie was smoking when he defined this behavior. I'm guessing at some kind of problem with the parser in an early version of the compiler.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I would guess that this probably simplified memory allocation in the first compilers, which was important when everything had to fit into 128k or less of of data space. In fact, in the very earliest C compilers, there was also a single *global* namespace for structure member names. This meant that `struct foo` and `struct bar` could not both have a member named `x`, which lead directly to the taglet style (`f_x`, `b_x`) for struct members that is common to this day in some C code. – Dale Hagglund Apr 11 '10 at 21:46
  • @Dave: I think you're right. Something was smoking with the structure member names. It's been too long, I put that brain cell in the ignore corner a long time ago. Wow, why did he have to? B? – Hans Passant Apr 11 '10 at 21:57
3

I like to do this in C:

// in a "public" header file
typedef struct Example Example;

// in a "private" header or a source file
struct Example { ... };

Like this, I have Example as an opaque type (i.e., I can only pass pointers to it about) throughout my code, except for the code that implements its operations, which can see what it is really defined as. (You could use a separate name for the opaque type but there's no real advantage to that.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
2

Its just for the code readability. typedefs are just to give new name for the data type. Instead of giving int every where you can name it the way you want and wherever you use int you can just replace it by the new name you have given. Same thing applies for structures also.

PrithviRaj
  • 571
  • 1
  • 7
  • 15
1

It's a form of information hiding and useful when creating opaque types. See this SO link for a slightly longer answer.

Community
  • 1
  • 1
Christoph
  • 164,997
  • 36
  • 182
  • 240
1

A non-typedefed struct name in C requires "struct" to be prepended everywhere the type name is used, so most people use typedef to create a new name for the type that does not need to have the struct keyword prepended all the time.

The reasons for doing this are code readability, reduced typing, and probably clarity as well, but typedefs can actually obscure information about pointer types.

In all honesty the need to typedef to create new names for structs is a relic, and it's a shame that C99 didn't follow C++'s lead and remove it.

Dan Olson
  • 22,849
  • 4
  • 42
  • 56
  • C99 couldn't get rid of having to `typedef` to get rid of the `struct` because of backward compatibility. There's lots of code like `struct Foo {...} ; typedef ... Foo;` which would stop compiling. – JeremyP Aug 01 '12 at 16:18
0

I actually don't use typedef for structs. Why? Because I would use some convention such a s_ prefix anyway, to easily see what kind of variable I'm looking at.

Since I use a lot of abstract data types, I guess it would be a good idea to use a typedef, so that the user really uses it as an opaque type. But in practice I always use a struct in the implementation anyway.

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95