Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top