Đức Nguyễn Đức Nguyễn -4 years ago 65
C Question

What is the difference node *node1 vs node* node1?

I have 1 node:

struct node
int key;
struct node *left, *right;

What is the difference between

node *node1


node* node1


Answer Source

Without a typedef, both the declarations are illegal.

With a typedef like

 typedef struct node node;

in place, there's no difference. Both the statements declare a variable node1 of type pointer to node. (Note: You'll still be needing the terminating ;, though).

It's a matter of choice, but some (including me) prefer to attach the pointer notation to the variable, to avoid misunderstanding, in case of multiple variable declaration, like

 node *p, q;

where, p is pointer type, but q is not.

Writing it like

 node* p, q;

may create the illusion that p and q both are of pointer type, where, in essence, they are not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download