generic swap can be written as -> #define Swap(T, a, b) {T temp = a; a=b;
b=temp;}this is working fine in gcc On Sat, Jul 9, 2011 at 1:45 PM, saurabh singh <[email protected]> wrote: > #include<stdio.h> > #include<error.h> > #include<malloc.h> > #include<stdlib.h> > void swap(void *p1,void *p2,const unsigned size) > { > char *buff=(char*) malloc(size*sizeof(char)); > if(!buff) > { > perror("malloc"); > exit(11); > } > memcpy(buff,p1,size); > memcpy(p1,p2,size); > memcpy(p2,buff,size); > free(buff); > return; > } > int main() > { > int a=12,b=24; > char c='a',d='b'; > float e=1.1f,f=2.3f; > char wrd1[]="Hello"; > char wrd2[]="Bye"; > char *p=wrd1; > char *q=wrd2; > swap(&a,&b,sizeof(int)); > swap(&c,&d,sizeof(char)); > swap(&e,&f,sizeof(float)); > swap(&p,&q,sizeof(p)); > printf("%d\t%d\n",a,b); > printf("%c\t%c\n",c,d); > printf("%f\t%f\n",e,f); > printf("%s\t%s\n",p,q); > return 0; > } > Its a generic function not a macro, I know but the idea would be same. > > > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
