Using array name/pointer to sort instances

Source: Internet
Author: User

An example of using an array name as a function parameter to pass a sort and sort with a pointer in C + +.

The following two examples are very important to note that the function passes not the true value of the array elements in the array but the actual address of the array in memory.

 #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 the */
int i;
for (i=0;i<10;i++)
{
printf ("%d", a);
}
printf ("\ n");
Reversal (a,10);//* Call custom number to display the reverse sort, and transmit the starting address of array A to formal parameter x */
for (i=0;i<10;i++)
{
printf ("%d", a);
}
printf ("\ n");

}

void reversal (x,n)
int x[],n;/* Define formal parameters */
{
int m= (n-1)/2;/* Calculate 10 number of times, because it is 22 to swap the first array is x[0 Therefore, it should be int (9/2) */
int temp,i,j;/* Establish zero variable temp to store x value at zero per Exchange processing */
for (i=0;i<=m;i++)
{
J=n-1-i; * Inverse calculation of the transposed array subscript, such as x[0] corresponding to the x[n-1-i] is x[9] */
Temp=x;
X=X[J];
X[j]=temp;
}
}

/* Secondary title Note: here because a[10] and x[10] are sharing the memory address bit, so the actual value of a[10] changed. */

#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 the */
int i;
for (i=0;i<10;i++)
{
printf ("%d", a);
}
printf ("\ n");
Reversal (a,10);/* Call the custom number to display the order in reverse, andThe start address of Group A is passed to the formal parameter x */
for (i=0;i<10;i++)
{
printf ("%d", a);
}
printf ("\ n");

}

void reversal (x,n)
int *x,n;/* define x as pointer variable */
{
int temp,*p,*i,*j; * It should be noted here that the temp stores the data temporarily with the exchange of */
I = x;/* Use the pointer variable I to store the starting address of array A */
P = x + ((n-1)/2);/* The address of array A */
J = x + N when calculating the last loop -1; /* Computes the end address of array A, i.e. a[9], for swapping */
for (; i<=p;i++,j--)//exchanging array element values using loops and pointers */
{
Temp=*i;/* temporary storage with temp * I is the actual value of a in the loop */
*i=*j;
*j=temp;
}
}
/* This example also notes that the operation of arrays using pointers also alters the value of the elements of the actual array */

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.