c - Swap two pointers using XOR -


i have quick question using xor 2 swap 2 string literals.

so have following:

#include <stdio.h> #include <stdlib.h> #include <string.h>   void intswap(int *a, int *b){     *a=*a^*b;     *b=*a^*b;     *a=*a^*b; }   void swapstring(char **a, char **b){     char *temp=*a;     *a=*b;     *b=temp; }  void main(){     char *s= "ha";     char *t= "oh";      printf("%s , %s \n",s,t); // prints ha oh     swapstring(&s,&t);     printf("%s , %s \n",s,t); // prints oh ha       int a=10;     int b=5;     printf("%d %d\n",a,b); //print 10 5     intswap(&a,&b);     printf("%d %d\n",a,b); //print 5 10 } 

as can see, used binary operation xor intswap. however, when tried same thing swapstring, it's not working.

i error message saying: invalid operands binary ^ (have ‘char *’ , ‘char *’)

do know how use xor swap 2 string literals? possible in c? ahead!!

there's no bitwise operations on pointers. operations can act on apart derefence + , -. need cast intptr_t , back.

anyway, it's bad practice , won't save cycles. compilers recognize swaps using simple assignments , optimize you.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -