C / C ++ in the case of three kinds of transfer function parameters (p * p & p)

 1 #include <stdio.h>
 2 void swap(int a,int b){
 3     int temp=a;
 4     a=b;
 5     b=temp;
 6 } 
 7 int main(){
 8     int a=1,b=2;
 9     swap(a,b);
10     printf("%d %d",a,b);
11     return 0;
12 }

method one:

This approach can not exchange more than two numbers.

This is because the function of the parameter in the process of receiving the one-way transmission disposable value, i.e., when the call swap (a, b) will pass in the value of a b, the equivalent pass in a copy of the copy operation is not performed ab will affect the value of the main function.

 

. 1 #include <stdio.h>
 2  void the swap ( int * A, int * B) {
 . 3      int TEMP * = A;
 . 4      * = A * B;
 . 5      * B = TEMP;
 . 6  } 
 . 7  int main () {
 . 8      int A = . 1 , B = 2 ;
 . 9      int * P1 = & A ; // pointer does not initialize dangerous
 10      int * P2 = & B;
 . 11      the swap (P1, P2);
 12 is      the printf ( " % D% D \ n- " , A, B);
 13 is     printf("%d %d\n",*p1,*p2); 
14     return 0;
15 }

Method Two:

This practice can be exchanged more than two numbers.

Because the pointer variable is the address, then use the pointer variable as a parameter passed in is the address . In the above formulas, the & a and & b passed as parameters, a pointer swap function in a storage & a pointer b stored & b, enables a direct data address stored in the operation, thus the switching operation will change the main function of a and the value of b.

Only in the case of obtaining the address of the operation of the data points to address in order to truly modify variables.

 

 1 #include <stdio.h>
 2 void swap(int* a,int* b){
 3     int *temp=a;
 4     a=b;
 5     b=temp;
 6 } 
 7 int main(){
 8     int a=1,b=2;
 9     int* p1=&a;
10     int* p2=&b;
11     printf("%d %d\n",*p1,*p2);
12     printf("%d %d\n",&a,&b);
13     swap(p1,p2);
14     printf("%d %d\n",*p1,*p2); 
15     printf("%d %d\n",&a,&b);
16     return 0;
17 }

Method three:

This approach can not exchange more than two numbers.

Because the nature and practice as a method, swap function to exchange the main function of a and b addresses fact has not been exchanged after completion of address. In fact, main function passed to swap the function of "address" is an "unsigned integer", which is itself just like ordinary variables passed by value .

 

 1 #include <stdio.h>
 2 void swap(int &x,int &y){
 3     int temp=x;
 4     x=y;
 5     y=temp;
 6 } 
 7 int main(){
 8     int a=1,b=2;
 9     printf("%d %d\n",a,b);
10     swap(a,b);
11     printf("%d %d\n",a,b);
12     return 0;
13 }

Method four:

This method can exchange the above two numbers.

Use C ++ in the "reference" can not produce a copy, but the original variable to an alias . Operation of the reference variable is the operating variables of the original, it can be modified to achieve the function of external variables in the function effect. That is, in the formula x = ay = b, so the xy ab modify fact, is modified.

 

 1 #include <stdio.h>
 2 void swap(int* &x,int* &y){
 3     int* temp=x;
 4     x=y;
 5     y=temp;
 6 } 
 7 int main(){
 8     int a=1,b=2;
 9     int* p1=&a;
10     int* p2=&b; 
11     printf("%d %d\n",*p1,*p2);
12     printf("%d %d\n",&a,&b);
13     swap(p1,p2);
14     printf("%d %d\n",*p1,*p2);
15     printf("%d %d\n",&b,&a);
16     return 0;
17 }

Method five:

This practice can be exchanged more than two numbers.

Because the pointer is referenced, a change is to change the pointer xy pointer p1 p2, i.e. the main function of the address ab interchanged. The program runs shown below.

 

Guess you like

Origin www.cnblogs.com/PennyXia/p/12346366.html