title: Creating arrays on the heap and addressing them with pointers
body: <p>I'm having trouble understanding the following bit of code that I was hoping would create an array on the heap and fill it with the characters 9 down to 0 (I know I could just index the array like a normal stack array with [] notation to do this but I'm doing it this way to try to understand pointers in more depth):</p>
  
  <pre><code>int *ptrHeapArray = new int[10]; 
  
      for(int f=9; f&gt;=0 ;f--)
      {
          *ptrHeapArray = f;
          ptrHeapArray++;
      }
      for(int f=0; f&lt;10; f++)
          cout &lt;&lt; ptrHeapArray[f]  &lt;&lt; "\n";
  </code></pre>
  
  <p>It prints out compleletly unexpected values.</p>
  
  <p>As I understand the above, the 'new' command creates an array on the heap and sends me back a pointer to the address where the array is. Since the pointer I assign (ptrHeapArray) is of int size I assumed I could use pointer post incrementing to navigate through the array. However the results indicate that my assumptions are wrong.</p>
  
  <p>This got me to thinking that perhaps the pointer passed back by the 'new' keyword is just a pointer to the whole array and can't be used to step through the array for some reason.
  So I tried creating another pointer to the pointer returned by the 'new' keyword and used that to do my array population:</p>
  
  <pre><code>int *ptrHeapArray = new int[10];  //array to hold FRANK data in 32 bit chunks
  int *ptrToHeapArrayPointer = ptrHeapArray;
  
  for(int f=9; f&gt;=0 ;f--)
  {
      *ptrToHeapArrayPointer = f;
      ptrToHeapArrayPointer++;
  }
  for(int f=0; f&lt;10; f++)
      cout &lt;&lt; ptrHeapArray[f]  &lt;&lt; "\n";   
  </code></pre>
  
  <p>This worked fine.
  Can anyone explain to me why I had to do this and couldn't just have used the pointer passed backed to me by the 'new' keyword?</p>
  
  <p>Thanks</p>