1

I have 1 node:

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

What is the difference between

node  *node1

vs

node*  node1

?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • The way C declaration syntax works, the "pointer-ness" of an object is specified as part of the *declarator*, not the type specifier. Given the declaration `int *p`, the `int`-ness of `p` is specified by the type specifier `int`, while the pointer-ness of `p` is specified by the *declarator* `*p`. The declaration is *parsed* as `int (*p);`, meaning a declaration like `int* p, q;` would be parsed as `int (*p), q;`. – John Bode May 18 '17 at 18:42

1 Answers1

1

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261