The reverse output array element - Pointer

Generally, we find an array output backwards, often using the following method

#include<stdio.h>
#include<stdlib.h>
void  instead (int x[],int n)
{
    int i,j,t;
    for(i=0,j=n-1;i<=j;i++,j--)
    {
          t=x[i];
          x[i]=x[j];
          x[j]=t;
    }
}
int main ()
{
    int i,a[10]={0,1,2,3,4,5,6,7,8,9};
    instead(a,10);
    printf("The array has been reverted:\n");
    for(int i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
}

But when you learn the pointer, you yourself will he get it, okay try to consolidate it.

I. Introduction The first, now showtime

. 1 #include <stdio.h>
 2 #include <stdlib.h>
 . 3  void   INSTEAD ( int * X, int n-) // will pay a first array address to this
 . 4  {
 . 5      int * I, * J, P * , T, m = (N- . 1 ) / 2 ; // I, J represents the address will access the array elements and a change (inversion array a)
 . 6      I = X; X + J = N- . 1 ; P = + X m; // the first address and the last position in the array of a given I, J
 . 7      for (; I <= P; ++ I, J, )
 . 8      {
 . 9          T * = I;
 10          * * I = J;
 . 11          = J * T;
 12 is      }
 13 is }
14 int main()
15 {
16     int i,a[10]={0,1,2,3,4,5,6,7,8,9};
17     instead(a,10);
18     printf("The array has been reverted:\n");
19     for(int i=0;i<10;i++)
20     {
21         printf("%d ",a[i]);
22     }
23     printf("\n");
24 }

Second, and then provide a second

#include<stdio.h>
#include<stdlib.h>
void  instead (int *x,int n)
{
    int *i,*j,*p,t,m=(n-1)/2;
    i=x;j=x+n-1;p=x+m;
    for(;i<=p;i++,j--)
    {
        t=*i;
        *i=*j;
        *j=t;
    }
}
int main ()
{
    int I, a [ 10 ], P * = a; // array a pointer to the address is stored, the letter A represents a single address array a.
    for (I = 0 ; I < 10 ; I ++, P ++ ) // move the pointer to the array a respective assignment.
    {
        scanf("%d",p);
    }
    p = a; // will be transferred back to the first address of a p array. 
    INSTEAD (P, 10 );
    printf("The array has been reverted:\n");
    for(int i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
}

Third, again a third representation

#include<stdio.h>
#include<stdlib.h>
void  instead (int x[],int n)
{
    int i,j,m=(n-1)/2,t;
    for(i=0,j=n-1;i<=j;i++,j--)
    {
        t=x[i];
        x[i]=x[j];
        x[j]=t;
    }
}
int main ()
{
    int i,a[10],*p=a;
    for(i=0;i<10;i++,p++)
    {
        scanf("%d",p);
    }
    p = a; // p will be transferred back to the first address of a array.
    instead(p,10);
    printf("The array has been reverted:\n");
    for(int i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
}

It is just a few chestnuts, casual look on the line.

Guess you like

Origin www.cnblogs.com/zhuyukun/p/12520597.html