title: How to swap memory between two void pointers in c
body: <p>I am trying to swap the memory that is pointed to by two void pointers. I get an error because I can't do a sizeof on a void, or size of an element of pa or pb (because you can't dereference a void pointer). The problem is I can't determine what data type pa and pb are going to be at runtime(this is supposed to swap any data type sent as parameters). How would I determine that so I can properly use malloc? Here is my code:</p>
  
  <pre><code>void SwapObjects(void *pa, void *pb, size_t size)
  {
      void *pc; 
      if ((pc = (malloc(size * sizeof(pa)) == NULL))) //this is wrong         {
          printf("Cannot allocate memory");
          return;
      }
      memcpy(pc, pa, size); //runtime error here
      memcpy(pa, pb, size);
      memcpy(pb, pc, size);
  }
  </code></pre>