首页 > 代码库 > C之交换数据案例

C之交换数据案例

//值传递

void swap(int i,int j){

  printf("交换后:\n ");

  int tmp;

  tmp = i;

  i = j;

  j = tmp; 

} 

//引用传递,传递地址

void swap1(int* p,int* q){

  printf("交换后:\n ");

  int tmp;

  tmp = *p;

  *p = *q;
  
  *q = tmp; 

} 

main(){

  int i =3;

  int j = 5;

  printf("i=%d \n",i); //i = 3

  printf("j=%d \n",j); //j = 5 

  //交换数值 

  swap1(&i,&j); 

  printf("i=%d \n",i); //i = 5

  printf("j=%d \n",j); //i = 3

}

 

 

C之交换数据案例