Using pointers to swap int array values

前端 未结 7 1352
走了就别回头了
走了就别回头了 2021-01-05 17:57

I am supposed to use pointers to swap ints in an array. It compiles with no errors or warnings and runs but does not swap the ints. Any suggestions would be helpful!!!

7条回答
  •  长情又很酷
    2021-01-05 18:28

    You can also swap the values without any temporary variable:

    void swap(int *x, int *y)
    {
       *x ^= *y;
       *y ^= *x;
       *x ^= *y;
    }
    

    then call:

    swap(&ary[0], &ary[1]);
    

提交回复
热议问题