[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.
// Variable int nNum; // int-type storage space declared as nNum = 100; // nNum storage value 100 nNum = 200; // change nNum storage value 200
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.
// Pointer int * pNum; // defines pointer pNumpNum = & nNum; // pNum points to the address of the variable nNum in the memory, instead of the value of the variable itself 200 (get the address operator &: the address of the variable. For example, & nNum is the address of the nNum variable) int nNum1 = * pNum; // the pointer pNum points to the bucket value 200 (pointer operator *: Retrieves data from the address. For example, * pNum is a value from the pNum address)
Pointer variable:The pointer is the address, and the pointer variable is the variable that stores the address.
// Pointer variable int * pVar = pNum; // equivalent to int * pVar = & nNum;
Level 2 pointer:Pointer to the pointer variable.
// Second-level pointer int ** 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.
int *pNum = NULL;...if(NULL != pNum){ ...}
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)
// Function minint min (int x, int y); // function maxint max (int x, int y); // function pointer int (* pFun) (int, int); // use the function pointer to call the function int nNumMin = 1; int nNumMax = 100; int nMin = 0; int nMax = 0; pFun = min; nMin = (* pFun) (nNumMin, nNumMax); // nMin = 1; pFun = max; nMax = (* pFun) (nNumMin, nNumMax); /// nMax = 100;
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 ))
Int * pfun (int, int); // pfun is a pointer to a function whose return value is int.
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.
Int I = 100; int & j = I; // indicates the ID, not the address.
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.