Why do these swap functions behave differently?

前端 未结 5 901
抹茶落季
抹茶落季 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:35

    swap1 will not work because the function just copied the arguments, not affecting the variables in main.

    swap2 will not work either.

提交回复
热议问题