When to use typedef in C?

Categories:  Programming
Labels:  C, C++, gcc

Can anybody tell me when to use typedef in C? In the following code I get a warning by gcc: warning: useless storage class specifier in empty declaration

1
2
3
4
5
typedef struct node
{
  int data;
  struct node* forwardLink;
};

So.. You can do this:

1
2
3
4
struct node {
  int data;
  struct node* forwardLink;
};

To define an object that you can use as struct node. Like this:

struct node x;

However, say you wanted to refer to it as just node. Then you could do:

1
2
3
4
5
6
struct node {
  int data;
  struct node* forwardLink;
};

typedef struct node node;

or

1
2
3
4
typedef struct {
  int data;
  void* forwardLink;
} node;

And then use that as:

node x;

Related Posts: