The parameter passed by the function is a copy of the original parameter.
It is impossible to use C language programming without using functions. But do we know whether to pass function parameters. This section describes the parameters passed by functions in C ..
Function parameters are generally divided into two types: Common variables and pointer variables. These parameters will be called by the function body. You can also input parameters that are never called by the function, just as you have declared some variables, but never used them, there is no syntax problem.
So what is the relationship between the passed parameters in the function body and the original parameters?
The parameter received by the function body is a copy of the original parameter.
1. Transfer of common variables in the function first let's look at the common variables. The parameters in the function body are copies of the common variables. The following is an example of the source program:
# Include <stdio. h>
# Include <stdlib. h>
Int test (INT T1, int T2 );
Int main (INT argc, char * argv [])
{
Int T1 = 10;
Int t2 = 0;
Printf ("[main] T1: % d \ TT1: % x \ n", T1, & T1 );
Printf ("[main] T2: % d \ TT2: % x \ n", T2, & T2 );
Test (T1, T2 );
Printf ("[main] T1: % d \ TT1: % x \ n", T1, & T1 );
Printf ("[main] T2: % d \ TT2: % x \ n", T2, & T2 );
System ("pause ");
Return 0;
}
Int test (INT T1, int T2)
{
Printf ("in func... \ n ");
Printf ("[test] T1: % d \ TT1: % x \ n", T1, & T1 );
Printf ("[test] T2: % d \ TT2: % x \ n", T2, & T2 );
T2 = T1;
Printf ("[test] After t2 = T1 \ n ");
Printf ("[test] T1: % d \ TT1: % x \ n", T1, & T1 );
Printf ("[test] T2: % d \ TT2: % x \ n", T2, & T2 );
Printf ("in func over... \ n ");
Return 1;
}
The execution result is:
[Main] T1: 10 T1: 22ff7c
[Main] T2: 0 t2: 22ff78
In func ....
[Test] T1: 10 T1: 22ff60
[Test] T2: 0 t2: 22ff64
[Test] After t2 = T1
[Test] T1: 10 T1: 22ff60
[Test] T2: 10 t2: 22ff64
In func over ....
[Main] T1: 10 T1: 22ff7c
[Main] T2: 0 t2: 22ff78
(The printed Address value may be different from the result I obtained .)
We can see that the values and addresses of T1 and T2 remain unchanged before and after being called by the test function. In the test function, the T1 and T2 addresses are not the same as those in the main function. They are only copies of the original T1 and T2. All operations on the copy will not affect the original parameters outside the test function.
2. pointer variables are passed in the function as variables in function transmission. Some special functions transmit a copy of common variables. For pointers, the function transmits a copy of the address it stores. The copy address is the same as the address stored in the original pointer.
Let's take a look at the example program:
# Include <stdio. h>
# Include <stdlib. h>
Int test (char * T1, char * t2 );
Int main (INT argc, char * argv [])
{
Char T1 [] = "kdsfkasdfkdsf ";
Char * t2 = NULL;
Printf ("[main] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[main] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
Test (T1, T2 );
Printf ("[main] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[main] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
System ("pause ");
Return 0;
}
Int test (char * T1, char * t2)
{
Printf ("in func... \ n ");
Printf ("[test] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[test] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
T2 = T1;
Printf ("[test] After t2 = T1 \ n ");
Printf ("[test] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[test] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
Printf ("in func over... \ n ");
Return 1;
}
Output result:
[Main] T1: kdsfkasdfkdsf T1: 22ff68 & T1: 22ff68
[Main] T2: (null) T2: 0 & t2: 22ff64
In func ....
[Test] T1: kdsfkasdfkdsf T1: 22ff68 & T1: 22ff40
[Test] T2: (null) T2: 0 & t2: 22ff44
[Test] After t2 = T1
[Test] T1: kdsfkasdfkdsf T1: 22ff68 & T1: 22ff40
[Test] T2: kdsfkasdfkdsf t2: 22ff68 & t2: 22ff44
In func over ....
[Main] T1: kdsfkasdfkdsf T1: 22ff68 & T1: 22ff68
[Main] T2: (null) T2: 0 & t2: 22ff64
(The printed Address value may be different from the result I obtained .)
As you can see, in the main function, the addresses stored in T1 and T2, and the value of the string corresponding to the address, are exactly the same as those transmitted in T1 and T2 in the test function, however, the T1 and T2 addresses are completely different,
L so the pointer passes a copy of its address in the function. You can modify the value of the address stored by the pointer in the function body, the modification is also effective for the original parameter in the function body because the original parameter also points to this address.
L The pointer can be modified in the function body to store the address, but its modification is invalid for the function in vitro, because it is only a copy of the address of the original pointer parameter, the original pointer still points to the original address.
3. Use a pointer to modify the address indicated by the pointer in the function body. If you must modify the address indicated by the pointer parameter, what should you do? In this case, we need a pointer. See the example program:
# Include <stdio. h>
# Include <stdlib. h>
Int test (char ** T1, char ** T2 );
Int main (INT argc, char * argv [])
{
Char * T1 = "tttt ";
Char * t2 = NULL;
Printf ("[main] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[main] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
Test (& T1, & T2 );
Printf ("[main] T1: % s \ TT1: % x \ t & T1: % x \ n", T1, T1, & T1 );
Printf ("[main] T2: % s \ TT2: % x \ t & t2: % x \ n", T2, T2, & T2 );
// Printf ("[main] T2: % s \ n", T2 );
System ("pause ");
Return 0;
}
Int test (char ** T1, char ** T2)
{
Printf ("[test] In func... \ n ");
Printf ("[test] * T1: % s \ TT1: % x \ n", * T1, T1 );
Printf ("[test] * t2: % s \ TT2: % x \ n", * t2, T2 );
* T2 = * T1;
Printf ("[test] After * t2 = * T1 \ n ");
Printf ("[test] * T1: % s \ TT1: % x \ n", * T1, T1 );
Printf ("[test] * t2: % s \ TT2: % x \ n", * t2, T2 );
Printf ("[test] In func over... \ n ");
Return 1;
}