Reputation: 100
I m studying Binary Search Tree just had little Doubt, here is struct to construct a node.
struct Node
{
int data;
Node* left, *right;
};
Now My doubt is when I m creating a new node why do I have to write
Node* node =new Node;
Why not
Node* node;
Upvotes: 1
Views: 88
Reputation: 71
Node* node;
You define a pointer, but the pointer points to nothing.
Node* node =new Node
You define a pointer and a Node object, and make the pointer point to the object.
Upvotes: 3
Reputation: 167
Node* node
this is a pointer which is just declared.
Accessing this pointer may give your garbage.
If you want to make pointer point to your own node object, you do by
Node* node = new Node;
Hope this clears!
Upvotes: 0