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

}

 

 

posted on 2017-02-19 13:09  LoaderMan  阅读(350)  评论(0编辑  收藏  举报

导航