title: Pointers and reference issue
body: <p>I'm creating something similar to structure list. At the beginning of main I declare a null pointer. Then I call insert() function a couple of times, passing reference to that pointer, to add new elements.</p>
  
  <p>However, something seems to be wrong. I can't display the list's element, <code>std::cout</code> just breaks the program, even though it compiler without a warning.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct node {
      node *p, *left, *right;
      int key;
  };
  
  void insert(node *&amp;root, const int key)
  {
      node newElement = {};
      newElement.key = key;
  
      node *y = NULL;
      std::cout &lt;&lt; root-&gt;key; // this line
      while(root)
      {
          if(key == root-&gt;key) exit(EXIT_FAILURE);
          y = root;
          root = (key &lt; root-&gt;key) ? root-&gt;left : root-&gt;right;
      }
  
      newElement.p = y;
  
      if(!y) root = &amp;newElement;
      else if(key &lt; y-&gt;key) y-&gt;left = &amp;newElement;
      else y-&gt;right = &amp;newElement;
  }
  
  int main()
  {
      node *root = NULL;
      insert(root, 5);
          std::cout &lt;&lt; root-&gt;key; // works perfectly if I delete cout in insert()
      insert(root, 2);
          std::cout &lt;&lt; root-&gt;key; // program breaks before this line
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>As you can see, I create new structure element in insert function and save it inside the root pointer. In the first call, while loop isn't even initiated so it works, and I'm able to display root's element in the main function.</p>
  
  <p>But in the second call, while loop already works, and I get the problem I described.</p>
  
  <p>There's something wrong with <code>root-&gt;key</code> syntax because it doesn't work even if I place this in the first call.</p>
  
  <p><strong>What's wrong</strong>, and what's the reason?</p>
  
  <p><strong>Also</strong>, I've always seen inserting new list's elements through pointers like this:</p>
  
  <pre><code>node newElement = new node();
  newElement-&gt;key = 5;
  root-&gt;next = newElement;
  </code></pre>
  
  <p>Is this code equal to:</p>
  
  <pre><code>node newElement = {};
  newElement.key = 5;
  root-&gt;next = &amp;newElement;
  </code></pre>
  
  <p>? It would be a bit cleaner, and there wouldn't be need to delete memory.</p>