title: Swapping objects using pointers
body: <p>I'm trying to swap objects for a homework problem that uses void pointers to swap objects.  The declaration of my function has to be:</p>
  
  <pre><code>void swap(void *a, void *b, size_t size);
  </code></pre>
  
  <p>I'm not looking for the exact code how to do it so I can figure it out by myself, but I'm not sure if I understand it correctly.  I found that one problem is by doing:</p>
  
  <pre><code>void *temp;
  temp = a;
  a = b;
  b = temp;
  </code></pre>
  
  <p>only changes what the pointers point to.  Is that correct?  If it is correct, why doesn't swapping pointers actually change the contents between *a and *b.  Because if your pointer points to something different, couldn't you dereference it and the objects would now be different?  </p>
  
  <p>Similarly, just switching the values like:</p>
  
  <pre><code>void *temp;
  *temp = *a;
  *a = *b;
  *b = *temp;
  </code></pre>
  
  <p>Is not correct either, which I'm not sure why.  Because again, it seems to me that the content is switched.</p>
  
  <p>Does swapping objects mean complete swapping of memory and value of what a pointer points to?</p>
  
  <p>So it seems like I have to use malloc to allocate enough space for my swap.  If I allocate enough memory for one object, assuming they are the same size, I don't really see how it is different than the other two methods above.  </p>
  
  <pre><code>void *temp = malloc(sizeof(pa));
  // check for null pointer
  temp = a;
  // do something I'm not sure of since I don't quite get how allocating space is any 
  // different than the two above methods???
  </code></pre>
  
  <p>Thanks!</p>