title: When should I pass a "reference to pointer" or a "pointer to pointer" rather then just a pointer to the function?
body: <p>I have been trying to implement <a href="https://en.wikipedia.org/wiki/K-d_tree" rel="nofollow noreferrer">k-d tree</a> for 2D data points using C++.</p>
  <p>My <code>Node</code> struct is as follow (here, each 2D point is in form of <code>std::vector&lt;float&gt; point</code>. So, point[0] would be <code>x</code> and <code>point[1]</code> would be <code>y</code>. Also, each <code>node</code> have some sort of <code>id</code>.):</p>
  <pre><code>struct Node {
      std::vector&lt;float&gt; point;
      int id;
      Node* left;
      Node* right;
  
      Node(std::vector&lt;float&gt; setPoint, int setId)
          : point(setPoint), id(setId), left(NULL), right(NULL) {}
  };
  </code></pre>
  <p>Here is my <code>KdTree</code> struct:</p>
  <pre><code>struct KdTree {
      Node* root;
  
       KdTree() : root(NULL) {}
  
      void insertHelper(Node* node, int depth, std::vector&lt;float&gt; point, int id) {
          if(node == NULL) {
              node = new Node(point, id);
              return;
          }
  
          uint currDim = depth % 2;
  
          if(point[currDim] &lt; node-&gt;point[currDim])
              insertHelper(node-&gt;left, depth + 1, point, id);
          else
              insertHelper(node-&gt;right, depth + 1, point, id);
      }
  
      void insert(std::vector&lt;float&gt; point, int id) {
          insertHelper(root, 0, point, id);
      }
  
      void printKdTreeHelper(Node* node) {
          if(node == NULL) 
              return;
  
          std::cout &lt;&lt; node-&gt;id &lt;&lt; &quot;: (&quot; &lt;&lt; node-&gt;point[0] &lt;&lt; &quot;, &quot; &lt;&lt; node-&gt;point[1] &lt;&lt; &quot;)\n&quot;;
          printKdTreeHelper(node-&gt;left);
          printKdTreeHelper(node-&gt;right);
      }
  
      void printKdTree() {
          printKdTreeHelper(root);
      }
  };
  </code></pre>
  <p>and here is <code>main</code>:</p>
  <pre><code>int main() {
      std::vector&lt;std::vector&lt;float&gt;&gt; points = {{-6.2, 7}, {-6.3, 8.4}, {-5.2, 7.1}, {-5.7, 6.3}};
  
      KdTree* tree = new KdTree;
  
      for (int i = 0; i &lt; points.size(); i++)
          tree-&gt;insert(points[i], i);
      
      tree-&gt;printKdTree();
  }
  </code></pre>
  <p>So, basically, I have been trying to <code>insert</code> points in <code>tree</code> and then print the <code>tree</code>. But nothing is being printed.</p>
  <p>The biggest confusion I have is that in the <code>insertHelper</code> function if I update <code>Node*</code> (<strong>pointer</strong>) to <code>Node*&amp;</code> (<strong>a reference to pointer</strong>) then everything works as expected i.e. <code>void insertHelper(Node* node ...)</code> does not work but <code>void insertHelper(Node*&amp; node ...)</code> works. I kind of got it but still, it's not quite/crystal clear. I mean through <code>Node*</code> I'm already passing a pointer to the function, so whatever I do in the function should reflect in <code>main</code> as well, right? After all, I'm passing a pointer not a variable by value, isn't it? Why there is a need of passing a reference to pointer -- <code>Node*&amp;</code>?</p>
  <p>In fact, somewhere I also found another solution: pass by a pointer to pointer i.e. <code>void insertHelper(Node** node ...)</code> and then deference it in the function body. However, that doesn't make sense to me either.</p>
  <p>While answering this question, if possible, please share further resources and/or references I can take a look into to improve my understanding.</p>
  <p>PS: I came already across some SO QnA on <strong>a reference to pointer</strong> but I couldn't correlate with any of them in the context of above problem/confusion!</p>