0

Is the following

typedef struct node {
    int data;
    struct node* next;
} node;

the only way one can define a struct so that one needn't write out struct inside of the rest of the program when using it?

I.e. by the above struct the following works just fine:

node* head = NULL;

But, is there another way to express the same struct that is generally considered better?

2
  • 1
    Explain: what do you mean by better ? Commented Feb 4, 2014 at 18:14
  • Less verbose without impacting comprehension negatively. Commented Feb 4, 2014 at 18:22

2 Answers 2

2

No. You could also do:

struct node {
    int data;
    struct node* next;
};
typedef struct node node;

'Better' isn't really a qualifier that can be applied to these; to my knowledge there is no advantage to one or the other.

Sign up to request clarification or add additional context in comments.

Comments

0

Nope! You're spot on. In C++ this isn't necessary, but in C some people (Linux kernel for example) where they prefer leaving things as structs (see here)

2 Comments

Thank you, that's an interesting link you've provided. Do you know why they do this?
here's the master himself explaining why: yarchive.net/comp/linux/typedefs.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.