void swap(void *a, void *b, size_t size) { 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 pointer can be used to store any kind of address,it is typecasted to char pointer so that one byte at a time can be swapped.
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
void swap(void *a, void *b, size_t size)
ReplyDelete{
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 pointer can be used to store any kind of address,it is typecasted to char pointer so that one byte at a time can be swapped.
/* Generic Swap macro*/
ReplyDelete#define swap(a, b, type) { type t = a; a = b; b = t; }
#define Swap(X,Y) { __typeof__ (X) T = X; X = Y; Y = T; }
ReplyDeleteint main()
{
int a=10,b=20;
Swap(a,b);
printf("%d %d",a,b);
return 0;
}
__typeof__ is a GNU C extension so we don't even need to pass type as third parameter.but it will be portability issue.
/* Generic Swap macro*/
ReplyDelete#define swap(a, b, type) { type t = a; a = b; b = t; }
will not run .. try it... and try to find the reason :)