title: C++ for each With a Pointer
body: <p>I am trying to use a pointer to an array inside of a for each loop in C++. The code below won't work because the "for each statement cannot operate on variables of type 'int *'". I'd prefer to use the new operator so that the array is on the heap and not the stack, but I just can't seem to figure out the syntax here. Any suggestions?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      int total = 0;
  
      int* array = new int[6];
      array[0] = 10; array[1] = 20; array[2] = 30;
      array[3] = 40; array[4] = 50; array[5] = 60;
  
      for each(int i in array) {
          total += i;
      }
  
      cout &lt;&lt; total &lt;&lt; endl;
  }
  </code></pre>