What is the use of typedef?

hks picture hks · Apr 2, 2010 · Viewed 90.1k times · Source

What is the use of typedef keyword in C ? When is it needed?

Answer

T.J. Crowder picture T.J. Crowder · Apr 2, 2010

typedef is for defining something as a type. For instance:

typedef struct {
  int a;
  int b;
} THINGY;

...defines THINGY as the given struct. That way, you can use it like this:

THINGY t;

...rather than:

struct _THINGY_STRUCT {
  int a;
  int b;
};

struct _THINGY_STRUCT t;

...which is a bit more verbose. typedefs can make some things dramatically clearer, specially pointers to functions.