title: Sorting Array of Pointers in C++
body: <p>Hoping I can get a little advice on a sorting method I made.</p>
  
  <p>The purpose of this code is to create a int pointer array and sort the pointers in that array by the contents of regular int array.  Then assign values for a different variable based on the location of the original int array.</p>
  
  <p>The strangeness I am experiencing with this code is that the test code which shouldn't effect anything as far as I know... IS actually effecting the contents of my pointers.
  Perhaps the values aren't changing but the way I'm writing the test code is causing errors.</p>
  
  <pre><code> //create array
   int c[8] = {3,1,5,7,8,2,6,4};
   //create pointer array
   int *newptr[8];
   for(int k = 0; k&lt;8; k++)
   {
       newptr[k] = &amp;c[k];
   }
  //sort pointer array
  for(int j = 0; j&lt;8; j++)
  {
      for(; j &gt; -1 &amp;&amp; *newptr[j] &lt; *newptr[j+1]; j--)
      {
          int *temp = newptr[j+1];
          newptr[j+1] = newptr[j];
          newptr[j] = temp;
      }
  }
  //set lookuplocation
  int lookuplocation;
  for(int i = 0; i&lt;8; i++)
  {
      cout &lt;&lt; *newptr[i];
  
      if(newptr[i] == &amp;c[0])
      {
          cout &lt;&lt; *newptr[i] &lt;&lt; endl;
  
          //If I use endl or \n to test the pointers values I end up with only
          //a part of the correct data. 
  
          cout &lt;&lt; "\nSuccess!\n";
          lookuplocation = 0;
      }
  }
  //Also for my last test sometimes the first element gets messed up as well
  //test arrays
  for(int k = 0; k&lt;8; k++)
  {
      cout &lt;&lt; "Element " &lt;&lt; k &lt;&lt; ": " &lt;&lt; *newptr[k] &lt;&lt; endl;
      cout &lt;&lt; "Element " &lt;&lt; k &lt;&lt; ": " &lt;&lt; newptr[k] &lt;&lt; endl;
  }
  </code></pre>