문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top