Pointers and references have been a difficult part of C + + and I've been puzzled for a long time, so let's talk about my simple understanding of references and pointers.
When we use a function, we often tangle the argument to the parameter, and the function's operation on the parameter does not change the value of the argument (that is, the corresponding variable of the main function), and the most typical example is swap (int a,int b). As we all know, the function will re-create a storage space for the variable when it is called, and this storage space will temporarily store the parameter's value, and if the function is called, the value of its corresponding storage space will be freed. For example, the following section of code:
voidSWAP1 (intAintb) { inttemp =0; Temp=A; A=b; b=temp;}voidSWAP2 (int*a,int*b) { inttemp; Temp= *A; *a = *b; *b =temp;}intMain () {inti =3, j =8; Swap1 (i, j); SWAP2 (&i, &j); return 0;}
When the main function calls Swap1 (int a, int b), the variable A and a is placed in a storage space, and the storage space is not related to the original i,j storage space. After the function is executed, the space where a A, B is temporarily stored is freed, meaning that the value of a/b does not exist, so this function does not affect the value of i,j in the main function, because the function does not operate on the corresponding address of the i,j. The value of a variable is changed only when the address of the variable is manipulated in the program. (Simple comprehension).
In the function swap2 (int *a, int *b), because the parameter in the function is the address of the I,J, the operation of the address (the function is to manipulate the value of the variable stored in the address) changes the value of the argument. The essence of the reference is to directly manipulate the address of the variable, so the reference can also change the value of the argument. Pointers and references have the same functionality in many cases, but there are some differences, which can be found in other places as well. More complex points are discussed below.
struct treenode{ Char val; * Left; * Right;} *tree;
void creat (Tree &t) { char val= " " ; CIN >> Val; if (val = = " # " ) T = NULL; else {T = new (TreeNode); T ->val = Val; creat (T ->left); creat (T ->right); }}
Above is the code to build a two-fork tree. The creat (tree &t) parameter is a reference, not a pointer. The reason why a pointer cannot be used is that the parameter is a pointer to the address of the variable pointed to by the pointer, that is, the address pointed to by T, after the function has been established, the value of the pointer T in the main function is not changed. This is consistent with the swap function, but the reference can change the value of T in the main function. If you want to manipulate the pointer, you can use a pointer to the pointer, which is to manipulate the address of the pointer T, not the address of the object that T refers to, so that the value of the argument is changed in the function. Simply put, the parameters of a function can change the value of an argument only when it is manipulated by the address.
A simple understanding of references and pointers