title: Pass by pointer & Pass by reference
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c">What are the differences between pointer variable and reference variable in C++?</a><br>
    <a href="https://stackoverflow.com/questions/334856/are-there-benefits-of-passing-by-pointer-over-passing-by-reference-in-c">Are there benefits of passing by pointer over passing by reference in C++?</a>  </p>
  </blockquote>
  
  
  
  <p>In both cases, I achieved the result.
  So when is one preferred over the other? What are the reasons we use one over the other?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  void swap(int* x, int* y)
  {
      int z = *x;
      *x=*y;
      *y=z;
  }
  void swap(int&amp; x, int&amp; y)
  {
      int z = x;
      x=y;
      y=z;
  }
  
  int main()
  {
      int a = 45;
      int b = 35;
      cout&lt;&lt;"Before Swap\n";
      cout&lt;&lt;"a="&lt;&lt;a&lt;&lt;" b="&lt;&lt;b&lt;&lt;"\n";
  
      swap(&amp;a,&amp;b);
      cout&lt;&lt;"After Swap with pass by pointer\n";
      cout&lt;&lt;"a="&lt;&lt;a&lt;&lt;" b="&lt;&lt;b&lt;&lt;"\n";
  
      swap(a,b);
      cout&lt;&lt;"After Swap with pass by reference\n";
      cout&lt;&lt;"a="&lt;&lt;a&lt;&lt;" b="&lt;&lt;b&lt;&lt;"\n";
  }
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>Before Swap
  a=45 b=35
  After Swap with pass by pointer
  a=35 b=45
  
  After Swap with pass by reference
  a=45 b=35
  </code></pre>