! clarifying earlier post

The swaps below shows wrong ways pple have tried to swap, the only correct one 
is swapgen(for generic) and 

swap.

/*CORRECT SWAPS*/


void swapgen(void *a, void *b, size_t size)
{
//generic way, notice the third argument
  char *ca, *cb;
  int i;
  ca = (char *)a;
  cb = (char *)b;
  for(i=0;i<size;*(ca+i)^=*(cb+i),*(cb+i)^=*(ca+i),*(ca+i)^=*(cb+i),++i);
}

void swap(int *x, int *y)
 {
        
           int temp;
           temp= *x;
           *x = *y;
           *y = temp;
   
 }




/* WRONG SWAPS BELOW **/

void swap0(void *A, void *B)
{
   void *Temp;
 
  Temp = A;
  A = B;
  B = Temp;
};

void swap1(void **x, void **y) {
    void *t = *x;
    *x = *y;
    *y = t;
}


void swap2(int *a,int* b){
    
    int* temp;
    temp=a;    
    a=b;    
    b=temp;
    
}
void swap3(int *a,int*b){
    
    int* temp;
    temp= a;    
    a=b;    
    b=temp;
    
}

 swap4(int a,int b)
  {
    a=a+b;
    b=a-b;
    a=a-b;
  }


 swap4b(int* a,int* b)
  {
//would fail, for example in swap(INT_MAX,INT_MAX);
    *a=*a+*b;
   *b=*a-*b;
    *a=*a-*b;
  }

I hope this covers all the swaps we've seen, if  I miss some right ones pls do 
add to them.
So we can understand where the fuss is.

The codes are also visible here
 http://www.pastebin.com/d36260a6e

L.O


      

[Non-text portions of this message have been removed]

Reply via email to