C Array Reverse order

Source: Internet
Author: User

One, standard switching mode

/****

* Standard switching mode

* Implement the inverse of the array, the principle is the array of the end-to-end elements to Exchange

***/

#define N 5;

int main () {

int Array[n] = {15,20,25,30,35}

int temp; Declaring temporary variables

int i;

for (i = 0;i<n/2;i++) {

The first and second values are exchanged with the N-i-1 value

temp = Array[i];

Array[i] = array[n-i-1];

Array[n-i-1] = temp;

}

printf ("Reverse: \ n");

for (i = 0;i < n;i++) {

printf ("%d\t", * (array + i));

}

}

Second, the pointer Exchange Mode

/****

* Pointer Exchange Mode

* Implement the inverse of the array, the principle is the array of the end-to-end elements to Exchange

***/

#define N 5;

int main () {

int Array[n] = {15,20,25,30,35}

int temp; Declaring temporary variables

int i;

int *ptr_array_start = array;

int *ptr_array_end = array + N-1;

while (Ptr_array_start>=ptr_array_end) {

Swap, the pointers are updated separately

temp = *ptr_array_start;

*ptr_array_start = *ptr_array_end;

*ptr_array_end = temp;

First element pointer to move backwards

ptr_array_start++;

End element pointer to move forward

ptr_array_end--;

}

printf ("Reverse: \ n");

for (i = 0;i < n;i++) {

printf ("%d\t", * (array + i));

}

}

C Array Reverse order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.