title: Why I can't change a value via passing pointer using new-operator in cpp?
body: <p>I wanted to changed a variable with passing the method. I used traditional C way. I wrote this code in Visual Studio 2010 with Visual C++. However it does not give expected result.</p>
  
  <p>Code have been a purpose but I changed it for easy understandability.</p>
  
  <pre><code>#include&lt;cstdio&gt;
  using namespace std;
  void exampleMethod(char *array) {
      array = new char[6];
  
      array[0] = 'h';
      array[1] = 'e';
      array[2] = 'l';
      array[3] = 'l';
      array[4] = 'o';
      array[5] = 0; // for terminating
  
      fprintf(stdout, "exampleMethod(): myArray is:%s.\n", array);
  }
  void main() {
      char* myArray = 0;
  
      exampleMethod(myArray);
  
      fprintf(stdout,"main(): myArray is:%s.\n", myArray);
  
      getchar(); // to hold the console.
  }
  </code></pre>
  
  <p>Output of this code is:</p>
  
  <pre><code>exampleMethod(): myArray is:hello.
  main(): myArray is:(null).
  </code></pre>
  
  <p>I don't understand why pointer value was not changed in <code>main()</code>. I know that it is pass by reference and I changed myArray's values with pointer. I also used new-operator to initialize that pointer.</p>
  
  <p>After that, I changed code, I initialized variable <code>myArray</code> in main method with new-operator. (before it is in <code>exampleMethod()</code>.)</p>
  
  <pre><code>void exampleMethod(char *array) {
      array[0] = 'h';
      array[1] = 'e';
      array[2] = 'l';
      array[3] = 'l';
      array[4] = 'o';
      array[5] = 0; // for terminating
      fprintf(stdout, "exampleMethod(): myArray is:%s.\n", array);
  }
  void main() {
      char* myArray = new char[6];;
      exampleMethod(myArray);
      fprintf(stdout,"main(): myArray is:%s.\n", myArray);
  }
  </code></pre>
  
  <p>Surprisingly, code is running properly. It gives this ouput:</p>
  
  <pre><code>exampleMethod(): myArray is:hello.
  main(): myArray is:hello.
  </code></pre>
  
  <p>Why did not previous code run in such a way that I expected? I compiled and run it with Visual Studio 2010 that is Visual C++ project. I also tried it with Visual Studio 2015.</p>