I have 1 node:
struct node
{
int key;
struct node *left, *right;
};
What is the difference between
node *node1
vs
node* node1
?
I have 1 node:
struct node
{
int key;
struct node *left, *right;
};
What is the difference between
node *node1
vs
node* node1
?
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.