c***2 发帖数: 838 | 1 In C, we can swap two items like this:
void swap(int *a,int *b)
{
int t = *a ;
*a = *b;
*b = t;
}
Sometimes in interviews, people may want a fancy swap like this:
void swap2(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Can anybody figure out what's wrong with swap2()? | f***g 发帖数: 214 | | C***y 发帖数: 2546 | 3 估计是这个了
我一直盯着指针看,看了半天也觉得没啥问题
看了指针只是幌子
【在 f***g 的大作中提到】 : Overflow?
| c***2 发帖数: 838 | 4 If a,b point to same address, swap2 will assign zero to that address.
It took me half hour to trace that out.
Overflow is another issue. | a******7 发帖数: 106 | 5 classic self-assignment case | C***y 发帖数: 2546 | 6 这个还真没看出来。。。
谢谢分享
【在 c***2 的大作中提到】 : If a,b point to same address, swap2 will assign zero to that address. : It took me half hour to trace that out. : Overflow is another issue.
|
|