title: C++ How to properly copy the value of a pointer
body: <p>So I'm having a bit of an issue, and need help with this beginner question. </p>
  
  <p>I've changed my example so that it makes sense. I should not have posted one while sleep deprived.</p>
  
  <p>I'm trying to copy a pointer (lets call it p1) in order to create a second pointer (lets call it p2).</p>
  
  <p>When p1 changes the object it is pointing at, I don't want p2 to change along with it, which is what appears to be happening in my program. </p>
  
  <p>Essentially, here is the issue I'm having....</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main(){
  
      std::cout &lt;&lt; "Pointer Test\n\n";
  
      int num1, num2;
      int * p1, * p2;
  
      num1 = 5;
      num2 = 10;
  
      p1 = &amp;num1;
      p2 = p1;
  
      std::cout &lt;&lt; "P1 is pointing at value " &lt;&lt; *p1 &lt;&lt; " (should be 5)\n";
      std::cout &lt;&lt; "P2 is pointing at value " &lt;&lt; *p2 &lt;&lt; " (should be 5)\n\n";
  
      *p1 = num2;     //Even though the value of P1 is changing, I don't want the value of P2 to change. I want it to stay on num1
  
      std::cout &lt;&lt; "P1 is pointing at value " &lt;&lt; *p1 &lt;&lt; " (should be 10)\n";
      std::cout &lt;&lt; "P2 is pointing at value " &lt;&lt; *p2 &lt;&lt; " (I want this to be 5, not 10)\n";
  
      return 0;
  }
  
   /* 
   Assignments I've tried to make this work...
  
   p2 = p1;      //didn't work - *p2 changes when *p1 does, which I am trying to avoid
   *p2 = *p1;    //didn't work - caused segfault
   *p2 = p1;     //didn't work - can't convert int to *int, so won't compile
   p2 = &amp;(*p1);  //didn't work - gave same result as p2 = p1
  */
  </code></pre>
  
  <p>Any enlightenment on this matter would be greatly appreciated.</p>