Example of using array names and pointers for sorting in c/c ++

Source: Internet
Author: User

Author:NeteaseSchoolProfessor of program, Guan Ning

Note that in the following two examples, the function is not passing the true values of the array elements but the actual address of the array in the memory!

/* Program Author: Guan Ning
Site: www.cndev-lab.com
All manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author */

# Include <stdio. h>

Void main (void)
{

Void reversal ();
Static int a [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};/* Create an array and initialize */
Int I;
For (I = 0; I <10; I ++)
{
Printf ("% d", );
}
Printf ("");
Reversal (a, 10);/* call the custom Number of Han to reverse display and sort, and send the starting address of array a to the form parameter x */

For (I = 0; I <10; I ++)
{
Printf ("% d", );
}
Printf ("");

}

Void reversal (x, n)
Int x [], n;/* defined form parameter */
{
Int m = (n-1)/2;/* The number of 10 must be iterated several times, because the first array is replaced by x [0], so it should be int (9/2 )*/
Int temp, I, j;/* the zero-time variable temp is used to store the x value at zero point each time a transaction is processed */
For (I = 0; I <= m; I ++)
{
J = n-1-i;/* reverse calculate the subscript of the replaced array, for example, x [0] corresponds to x [n-1-i] is x [9] */
Temp = x;
X = x [j];
X [j] = temp;
}
}

/* Note the following: here, because a [10] and x [10] share the same memory address bit, the actual value of a [10] is changed after switching */

/* Program Author: Guan Ning
Site: www.cndev-lab.com
All manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author */

# Include <stdio. h>

Void main (void)
{

Void reversal ();
Static int a [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};/* Create an array and initialize */
Int I;
For (I = 0; I <10; I ++)
{
Printf ("% d", );
}
Printf ("");
Reversal (a, 10);/* call the custom Number of Han to reverse display and sort, and send the starting address of array a to the form parameter x */

For (I = 0; I <10; I ++)
{
Printf ("% d", );
}
Printf ("");

}

Void reversal (x, n)
Int * x, n;/* defines x as the pointer variable */
{
Int temp, * p, * I, * j;/* Note that temp temporarily stores data during use and exchange */
I = x;/* use the pointer variable I to store the starting address of array */
P = x + (n-1)/2);/* calculate the address of array a in the last loop */
J = x + n-1;/* calculate the end address of array a, that is, a [9], which is used for switching */
For (; I <= p; I ++, j --)/* Exchange array element values using loops and pointers */
{
Temp = * I;/* use temp to temporarily store * I, that is, the actual value of a in the loop */
* I = * j;
* J = temp;
}
}

/* In this example, we also need to note that the array operation by using pointers also changes the values of the actual array elements */

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.