In terms of concept. Essentially, a pointer is a variable that stores the variable address. It is logically independent and can be changed, this includes changes to the address it points to and the data stored in the address it points. Reference is an alias. It is not logically independent and its existence is dependent. Therefore, the reference must be initialized at the beginning, in addition, the referenced object cannot be changed throughout its lifecycle (it can only be attached to the same variable from start to end ). InC ++In, pointers and references are often used to pass function parameters. However, pointer passing parameters and reference passing parameters are essentially different: A pointer passing parameter is essentially a value passing method, which transmits an address value. During the value transfer process, parameters in the form of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, it becomes a copy of the real parameter. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function. During the reference transfer process, the form parameters of the called functions, although also opened up the memory space in the stack as local variables, however, the address of the Real Variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect addressing. That is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function. Reference transfer and pointer transfer are different, although they are all a local variable in the space of the called function stack, however, any processing of referenced parameters will operate the relevant variables in the main function in an indirect addressing method. If you change the pointer address in the called function for pointer passing parameters, it will not affect the variables related to the main function. If you want to change the relevant variables in the main function by passing the pointer parameter, you must use a pointer to the pointer or pointer reference. To further deepen the differences between pointers and references, I will explain the differences between them from the compilation perspective: ProgramDuring compilation, the pointer and reference are respectively added to the symbol table. The symbol table records the variable name and the address corresponding to the variable. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value referenced in the symbol table is the address value of the referenced object. The symbol table will not be changed after it is generated, so the pointer can change the object to which it points (the value in the pointer variable can be changed), but the referenced object cannot be modified. Finally, we will summarize the similarities and differences between pointers and references: ★Similarities: ● All are addresses; The Pointer Points to a piece of memory. Its content refers to the memory address, and the reference is the alias of a piece of memory. ★Differences: ● A pointer is an entity, and a reference is only an alias; ● The reference can only be initialized once during definition, but cannot be changed afterwards; the pointer can be changed; the reference can be "from one end", and the pointer can be "Thinking Differently "; ● No referenceConst, Pointer hasConst,ConstPointer; ● The reference cannot be blank or the pointer can be blank; ●"SizeofReference "to get the variable pointed(Object)And"SizeofPointer "gets the pointer size; ● Auto-increment of pointer and reference(++)Different computing meanings; ● The reference is type-safe, and the pointer is not ( more type checks than pointers |