指针11 指针作函数的参数1

函数的参数不仅可以是整型,实型,字符型等基本数据类型的数据,还可以是指针类型数据。指针做函数参数的作用是将一个变量的地址传送到另一个函数中,这样,形参指针就指向了主调函数中的变量,从而可以改变主调函数中变量的值。

举例如下
题目:输入两个整数,应用指针变量和函数,按从小到大排序输出这两个整数。

#include<stdio.h>
void swap(int *p1,int *p2)
{int temp;
 temp=*p1;
 *p1=*p2;
 *p2=temp;
}
int main()
{int a=0,b=0;
 int *point_a,*point_b;
 point_a=&a;
 point_b=&b;
 printf("Input a and b;\n");
 scanf("%d%d",&a,&b);
 if(a>b)
 	swap(point_a,point_b);
 printf("%d %d\n",a,b);
 return 0;
}

运行结果
在这里插入图片描述
下一篇
指针11 指针作函数的参数2

猜你喜欢

转载自blog.csdn.net/weixin_43918004/article/details/85047843
今日推荐