title: Altering an array from a function in C++
body: <p>I am new to C++ (the usual intro for every newbie XD) and I found this unexpected behavior. I traced the variables and arrays in my program until I determined this pattern:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  void showArray(int arr[], int n)
  {
      for(int i = 0; i &lt; n; i++) cout &lt;&lt; arr[i] &lt;&lt; " ";
      cout &lt;&lt; endl;
  }
  void someFunction(int x[], int n) // changes the original values
  {
      x[0] = 2;
      x[1] = 1;
      x[2] = 0;
  } 
  void someFunction2(int * x, int n)
  {
      x[0] = 2;
      x[1] = 1;
      x[2] = 0;
  } // changes the original values
  int someFunction3(int x[], int n)
  {
      x[0] = 2;
      x[1] = 1;
      x[2] = 0;
      return 0;
  } // changes the original values
  int someFunction4(int x[], int n)
  {
      x = new int[n];
      x[0] = 2;
      x[1] = 1;
      x[2] = 0;
      return 0;
  } // does NOT change the original value
  
  int main(void)
  {
      int * y = new int[3];
      y[0] = 0;
      y[1] = 1;
      y[2] = 2;
      showArray(y, 3);
      someFunction4(y, 3);
      showArray(y, 3);
      return 0;
  }
  </code></pre>
  
  <p>Why doesn't <code>someFunction4()</code> change the array <code>y</code> in <code>main()</code>? When I call the other <code>someFunctionX()</code> in <code>main()</code> instead, <code>y</code> successfully changes from <code>{0, 1, 2}</code> to <code>{2, 1, 0}</code>.</p>