通过指针实现数组最大值,最小值的地址输出和交换

1 #include<stdio.h>
2 #define N 5
3 int *pMax(int *arr,int n){
4 int *p=NULL,*temp1=arr+1;
5 for(p=arr+1;p<(arr+n);p++){
6 if(*temp1<*p)
7 temp1=p;
8 }
9 return temp1;
10 }
11 int *pMin(int *arr,int n){
12 int *p=NULL,*temp=arr+1;
13 for(p=arr+1;p<(arr+n);p++){
14 if(*temp>*p)
15 temp=p;
16 }
17 return temp;
18 }
19
20 void calPswap(int *a,int *b){
21 int t=*a;
22 *a=*b;
23 *b=t;
24 }
25
26 int main(int argc,const char *argv[]){
27 int arr[N]={12,15,16,4,18};
28 int * pmax=NULL;
29 int * pmin=NULL;
30 pmax=pMax(arr,N);
31 pmin=pMin(arr,N);
32 printf(“the Max is %d\n”,*pmax);
33 printf(“the Min is %d\n”,*pmin);
34 calPswap(pmax,pmin);
35 printf(“the Max is %d\n”,*pmax);
36 printf(“the Min is %d\n”,*pmin);
37
38 return 0;
39 }
实验结果:
在这里插入图片描述

发布了21 篇原创文章 · 获赞 16 · 访问量 3778

猜你喜欢

转载自blog.csdn.net/qq_40632341/article/details/89305028
今日推荐