-
Program 1 can realize the exchange of a and b values, but program 2 does not. To understand this, you need to know a few things:
1. The scope of action of the variable; Local variables (void swap(a,b) in program 2 The variables a, b, and k in the subroutine are all local variables, and the life cycle starts from calling this function, and the variables are logged off after the function is executed.
2. The way of transferring function parameters; The above two subroutines pass the parameters by passing values--- copying the values of the arguments to the parameters. However, the operation of the form parameters in the subroutine does not affect the actual parameters.
3. The meaning of the pointer variable; A pointer variable is a variable that points to the address of a variable (shaper, floating-point, character, etc.). That is, the value of the pointer variable is the address of a variable.
The following analysis: Program 1: After calling the subprogram, the form parameter obtains the value of the parameter through the value passing, both the form parameter and the parameter save the address of the same variable, and in the subprogram, the operation of the form parameter is indirectly to the pointed variable, not the value of the form parameter itself.
It is possible to change the values of a and b.
Program 2: In the subroutine, all operations are performed on the value of the parameter itself, and when the execution is completed, the parameter (local variable) is logged out.
Procedure 2:
-
is a copy. The reason why the latter program cannot implement the exchange is that the C C++ function parameter passes the value (the parameter that copies the value of the argument), so the operation of the argument in the function will not affect the corresponding argument.
The former program is able to implement the swap because the function does not operate on the parameters, but on the memory that the parameters refer to, and the memory that the parameters refer to is the memory that the corresponding arguments refer to.
-
Mostly functions.
void swap(int a, int b). void swap(int *p1,int *p2), the previous function: just passes the argument to the argument, and does not return (the value is passed) the following function:
It is to pass the address of the argument to the parameter, and the argument actually operates the address of the argument in the program, so the value under the address is exchanged in the program, which also serves the purpose of exchange.
-
There is no reference in standard C, only pointer parameters can be used.
C++ introduces pointer parameters to reduce the risk of pointer flexibility, as pointer parameters can not only pass a reference to data into the function, but also change the pointer within the function, i.e. inside the function to change the variables (not just objects) of the calling domain.
Compared to references, pointers have these characteristics:
Pointer variables can be repeatedly assigned or changed (not for references, once assigned, they cannot be changed) and pointer variables can be empty (references must point to specific objects).
If you don't use these features and find them troublesome, and are okay with non-pure C (i.e. you don't need to support supersets of other Cs like Objective-C), then use reference arguments, and even add const for further constraints if needed.
-
C++ can't pass an array directly to a function, but you can use the array name as an array pointer to a function.
There are three ways for a function to receive a pointer, int * pointer.
int [10] fixed-length array form.
int is an indefinitely long array.
The above three effects are the same, they are all receiving pointers, so they are all pointers. 3 is a quote.
It is important to note that int a[10] is used as a definition to create an array of 10 integer elements. However, it is used to receive integer pointers when used for form parameters, and there is no strict limit on the length, and it will not be checked during compilation.
For example, the following program can be compiled and run normally.
The parameter dimension is 1
void fun(int p[1])
main()
int a[100];Create an array of 100 elements to ensure that the fun function doesn't go out of bounds.
fun(a);
cout <
-
The first is, the second and fourth are the same, and the third is the quote. Please agree!
-
The second one is not, the rest are.
-
fun (int a[10] ) This one is not, everything else is.
To change the value of a variable by passing a function, you must pass an address. Your program is passing the value of the variable swap(a,b); If you want to change the value of a variable, you must pass the address of the variable, or do it by a pointer, if it is an array, you can directly use the array name, but if it is an element of the array, you still need to use the address. Just change it like the one upstairs did him. >>>More
Hello landlord, read shen (first tone).
Let me start with a brief introduction to this nakshatra. Betelgeuse. >>>More
Yishan ginseng is a type of mountain ginseng. There are two kinds of moving ginseng: "mountain migration" and "home migration": "mountain migration" means that when mountain farmers release the mountain, they find that the wild ginseng seedlings are not suitable for use due to their small weight and ginseng age, so they concentrate on transplanting them to the mountains and forests near their homes, which are easy to guard and make them grow naturally; "Home migration" means that ginseng farmers transplant the ginseng seedlings planted in the garden to the mountains and forests, without watering or fertilizing, so that they can grow naturally in the wild environment. >>>More
The address assigned to the parameter and the parameter is different. >>>More
Dai Yuan refers to (Feet on the ground
1. Wear a round track. >>>More