C-language functions exchange two numbers of values.
Code:
1 #include <stdio.h> 2 3 void swap(int x,int y) 4 { 5 int temp; 6 7 temp = x; 8 x = y; 9 y = temp;10 printf("In swap: x = %d,y = %d\n",x,y);11 }12 13 void swap_with_pt(int * x,int * y)14 {15 int temp;16 17 temp = *x;18 *x = *y;19 *y = temp;20 printf("In swap_with_pt: x = %d,y = %d\n",*x,*y);21 }22 23 int main()24 {25 int x = 5,y = 10;26 27 printf("Before swap: x = %d,y = %d\n",x,y);28 swap(x,y);29 printf("After swap: x = %d,y = %d\n",x,y);30 printf("-------------------------------\n");31 printf("Before swap_with_pt: x = %d,y = %d\n",x,y);32 swap_with_pt(&x,&y);33 printf("After swap_with_pt: x = %d,y = %d\n",x,y);34 35 return 0;36 }
Output:
1 Before swap: x = 5,y = 102 In swap: x = 10,y = 53 After swap: x = 5,y = 104 ---------------------------------5 Before swap_with_pt: x = 5,y = 106 In swap_with_pt: x =10,y = 57 After swap_with_pt: x = 10,y = 5
Explanation:
When the swap function is called, we first create a copy of the variables x and y, which we call x2, y2 (in fact, the names are still x and y, but here x and y only make sense in the swap function), and pass the value of x and y in the main function to the variable x2, y2. Then the function exchanges the values of x2 and y2, after the function is run, x2 and y2 variable end of life. Therefore, the values of x and y remain unchanged.
When the swap_with_pt function is called, it also creates a copy of the variables of the x and y pointers, which we call x2, y2 (in fact, the names are still x and y, but here x and y only make sense in the swap_with_pt function), and pass the address values of x and y to x2 and y2. At this time, x2 and y2 point to x in the main function, and then use the * operation to modify the values of x and y. Note that "* + pointer name" can change the value of the variable to which it points.
C language defines a function to realize the function of exchanging two integers (using pointers as function parameters)
# Include <stdio. h>
Void swap (int * p, int * q)
{Int t;
T = * p; * p = * q; * q = t;
}
Main ()
{Int a = 5, B = 4, * p, * q;
P = & a; q = & B;
Swap (p, q );
Printf ("a = % d B = % d", a, B );
Return 0;
}
..
This is written in C language. Why is it wrong to use a function to convert two numbers?
# Include <stdio. h>
Void huhuan (int * a, int * B)
{
Int t;
T = *;
* A = * B;
* B = t;
}
Int main ()
{
Int a = 3;
Int B = 5;
Huhuan (& a, & B );
Printf ("a = % d, B = % d", a, B );
}
Your understanding of the function is not thorough enough. First, the function can only return one value.
Return a; return B; if you write this statement, only a will be returned and return B will not be executed, because the return function has ended.
Function call process: the function parameters are the memory allocated in the stack, and the function call process is the value transfer process.
That is to say, the values of real parameters and form parameters are equal, but the storage space is different. You exchanged the values of a and B in the called functions,
The real parameters in the main function are not changed. The variable address should be passed through the pointer.
In this way, the operation on the same memory is performed in the call function.