title: Understanding pointers with a swap program in C
body: <p>I am trying to better understand pointers and referencing in C, and my course provided the following program as an example. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void swap(int* a, int* b);
  
  int main(void)
  {
      int x = 1;
      int y = 2;
  
      swap(&amp;x, &amp;y);
      printf("x is %i\n", x);
      printf("y is %i\n", y);
  }
  
  void swap(int* a, int* b)
  {
      int tmp = *a;
      *a = *b;
      *b = tmp;
  }
  </code></pre>
  
  <p>I shambled together the following to see if it would help me understand better what's happening, mainly in regards to the need to use &amp; versus *(dereference). Basically, the syntax of declaring a pointer to int type (int* a) versus using an asterisk to "dereference" (*a = *b) is quite confusing to me, and I was hoping someone could enlighten me. Here's another version of the above that I thought would help clarify, but really doesn't:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void swap(int* a, int* b);
  
  int main(void)
  {
      int x = 1;
      int y = 2;
      int *a = &amp;x;
      int *b = &amp;y;
  
      swap(a, b);
      printf("x is %i\n", x);
      printf("y is %i\n", y);
  }
  
  void swap(int* a, int* b)
  {
      int tmp = *a;
      *a = *b;
      *b = tmp;
  }
  </code></pre>
  
  <p>In short, my question is, is there a functional difference between what these two programs are doing? What is the difference between a dereference (<code>*a = *b</code>) versus using the <code>&amp;</code> operator (<code>*a = &amp;x</code>)". </p>