简体   繁体   中英

Difference between int* a and int* a =new int

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;
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.

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

  1. creating new node object
  2. assign a pointer to it so that you can reference this node later in code using the node pointer like this Node* node = new Node;

Hope this clears!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM