指针

来源:互联网 发布:网易邮箱smtp端口 编辑:程序博客网 时间:2024/05/09 02:11

#import <Foundation/Foundation.h>



int main(int argc,const char * argv[]) {

    //  输入10个整数,将其中最⼩小的数与第⼀个数对换,最⼤的数和最后⼀个数对换,指针实现

       int a[10]={0};

       for (int i=0; i<10; i++) {

            a[i]=arc4random()%100+1;

           printf("%d ",a[i]);

        }          //随机生成101--100之间的数放到数组a

        printf("\n..................................\n");

          int *p = a;  //a放到指针p

       int max = 0;//放最大值

       int min = 100;  //放最小值

       int maxIndex = 0//最大值的下标

       int minIndex = 0;//最大值的下标

       for (int i=0; i<10; i++) {

           if (*(p+i )>max) {

                max = *(p+i );

                maxIndex=i;

            }

           if (*(p+i )<min ) {

                min  = *(p+i );

                minIndex=i;

            }

    

        }

       int temp=max;

        *(p+maxIndex)=*(p+9);

        *(p+9)=temp;

    

       int temp1=min ;

        *(p+minIndex)=*p;

        *p = temp1;

       for (int i=0; i<10 ; i++) {

           printf("%d ",*(p+i ));

    }

    return 0;

}


0 0