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

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 }
实验结果:
在这里插入图片描述

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值