regarding question to the generic macro to swap int, float, double # define swap(a,b) ((a)^=(b)^=(a)^=(b))
On Jul 12, 10:08 pm, Aniket Dutta <aniketdutt...@gmail.com> wrote: > c=a+b > then find b=c-a; > if this b equals previous one then ok else overflow > > On 7/9/11, John Hayes <agressiveha...@gmail.com> wrote: > > > regarding question related to overflow > > > int ovr_sum(int *sum,int a,int b) > > { > > *sum=a+b > > if(*sum<0) > > return -1; //overflow > > else > > return 0; //no problem with the sum (overflow don't occur) > > } > > On Sat, Jul 9, 2011 at 2:05 PM, John Hayes <agressiveha...@gmail.com> wrote: > > >> 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 <saurab...@gmail.com> 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 algogeeks@googlegroups.com. > >>> To unsubscribe from this group, send email to > >>> algogeeks+unsubscr...@googlegroups.com. > >>> 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 algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > 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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.