【c语言】利用指针进行两个数的交换。

#include<stdio.h>
void swap(int *a,int *b){
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
void main(){
    int a,b,*m,*n;
    a=4;
    b=5;
    m=&a;
    n=&b;
    printf("a=%d,b=%d\n",a,b);
    swap(m,n);
    printf("a=%d,b=%d\n",a,b);

}

备注:就是在swap里面temp这个额外空间不能写成指针变量。。必须写成整形变量。

 

posted on 2014-12-21 22:16  蜘蛛牛牛  阅读(1576)  评论(0编辑  收藏  举报

导航