Frank Frank - 9 months ago 36
C Question

C typedef linked list

Supposed I have a struct with pointers to variables of the same type, for example for a simple linked list implementation.

Which of these two approaches is the most correct one?

EDIT: assume I have previously declared

typedef struct list_ list;
, otherwise Option 2 will not compile

Option 1:

typedef struct _list
{
int num;
struct _list *next;
} list;


Option 2:

typedef struct _list
{
int num;
list* next;
} list;


Both compile and work, so which should I use?

Answer Source

There is no real 'correct' answer to this question; it's a matter of personal preference. As with all style issues, I would say the following:

  • Be consistent
  • If you are coding in a group, agree a style with the rest of the group.