[C ++ self-explanation] basic Series 1 pointer and reference 0 Preface
Pointer, reference, pointer and reference are different.
1 pointer
Variable:In the code, you often apply for and name a bucket by defining variables, and use the bucket by the variable name.
nNum; nNum = ; nNum = ;
Pointer:It is also called a level-1 pointer to indicate a variable with a memory address. For a type T (int, char, folat...), T * is the pointer type pointing to T. a t * type variable stores the address of a T object.
*pNum; pNum = &nNum;int nNum1 = *pNum;
Pointer variable:The pointer is the address, and the pointer variable is the variable that stores the address.
*pVar = pNum;
Level 2 pointer:Pointer to the pointer variable.
**ppNum = pVar;
Use a graph to represent the second-level pointer:
C is A constant, B is A level-1 pointer, B's value (that is, the address of C) is level-1 pointer data, A is A level-2 pointer, and A's value (that is, the address of B) is the second-level pointer data.
Null Pointer:Does not point to any object. The pointer variable value is NULL. Null Pointer is often used to avoid invalid use of pointer variables.
*pNum =(NULL !=
Function pointer:Is the pointer variable pointing to the function. You can call a function using this pointer variable, just like referencing other types of variables with a pointer variable.
Function pointer declaration format: type specifier (* function name) (parameter)
min( x, max( x, (*pFun)(, nNumMin = nNumMax = nMin = nMax = == (*pFun)(nNumMin,nNumMax);pFun == (*pFun)(nNumMin,nNumMax);
Pointer function:The Return Value Type of a function is a pointer type, which is essentially a function.
Pointer function declaration format: type specifier * function name (parameter) or type specifier * (function name (parameter ))
*pfun(, );
2 references
Reference:Is the alias of an object, and the referenced operation is the same as the direct operation on the variable. It is mainly used for function parameters and return value types. T & indicates T type reference. A reference is not a data type and does not occupy storage space.
i = &j = i;
3. Differences between pointers and references
1) the pointer can point to a null value and cannot be referenced. If a variable points to another object, it may be null and should be a pointer; it cannot be null and should be referenced.
2) the pointer can be changed and the reference cannot be changed. The reference cannot be changed after initialization, And the pointer can change the object.
3) the pointer size is a fixed byte (4 bytes), and the reference size is the variable size.