#include"stdafx.h"intmain(int argc,char* argv[]){int a,b;//定义两个整形变量int*pointer_1,*pointer_2;//定义两个指针变量
pointer_1=&a;
pointer_2=&b;//将a和b的地址赋个指针变量printf("please input the values of a and b.\n");scanf("%d%d",&a,&b);if(a<b){
pointer_2=&a;
pointer_1=&b;}//a<b,则交换指针所指向的地址printf("max=%d min=%d\n",*pointer_1,*pointer_2);return0;}
方法二:交换指针变量所指变量的值
#include"stdafx.h"voidswap(int*x,int*y);intmain(int argc,char* argv[]){int a,b;int*pointer_1,*pointer_2;
pointer_1=&a;
pointer_2=&b;printf("please input the values of a and b:\n");scanf("%d%d",&a,&b);if(a<b)swap(pointer_1,pointer_2);//a<b,则将指针实参的值传递个指针形参printf("max=%d,min=%d\n",a,b);//a,b的值在上一条语句执行完后完成互换return0;}voidswap(int*x,int*y)//定义一个函数实现指针变量所指变量值得交换{int temp;
temp=*x;*x=*y;*y=temp;}