Why do these swap functions behave differently?

前端 未结 5 903
抹茶落季
抹茶落季 2020-12-21 12:11
#include 

void swap1(int a, int b)
{
    int temp = a;

    a = b;
    b = temp;
}

void swap2(int *a, int *b)
{
    int *temp = a;

    a = b;
    b         


        
5条回答
  •  情歌与酒
    2020-12-21 12:34

    Just for fun, exchange values without the use of a temporary variable

    x = x ^ y
    y = x ^ y
    x = x ^ y
    

提交回复
热议问题