title: How do I pass an array to function by reference?
body: <p>I want to pass array to function by reference. This function will  dynamically allocate the elements.</p>
  
  <p>This code give me an error message: <code>access violation</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std; 
  
  void func(int* ptr)
  {
      ptr = new int[2]; 
      ptr[0] = 1; 
      ptr[1] = 2; 
  }
  
  void main()
  { 
      int* arr = 0;
      func(arr);
      cout &lt;&lt; arr[0] &lt;&lt; endl; // error here 
  }
  </code></pre>