Article Directory
First, understanding pointers basis, an important index of 5 stars
Consisting of a variable: variable address, variable names, variable values
The int i = 10
Second, Pointer
2.1 What is
A pointer for storing the address in memory of variable, this variable by a pointer indirection
Such as: int * p; declare an integer pointer variable p, point to a variable of type int
Code Example
implementation pointer variable p to point i
int i= 10;
int *p; //声明一个整型指针变量p
p=&i;//将i的地址赋给指针变量p
p = & i, the & taken as addresses, the address of the variable i taking
Illustrates the code
because the pointer variable itself is a variable, variable address there, the variable names, variable values
can be seen from FIG pointer variable p value is stored to the variable i
2.2 How to use the pointer manipulated variable
* P use to represent the value of the variable i is a variable, that variable * p value by changing the variable i
Specific code
int i= 10;
int *p; //声明一个整型指针变量p
p=&i;//将i的地址赋给指针变量p
*p=11;
cout << i << endl;//输出i=11
* Supplement use
- int * p (* Variable name type name): * indicates a pointer type
- P * (* variable name): * indicates indirection operator
2.3 Why use a pointer
There are children's shoes might ask, can the variable name i directly change a variable value, and why it through a pointer
In fact, by changing the value of the variable variable name, but also by the middle of addresses to visit
The following operation from the perspective of the stack
For example
int *ip; /* 一个整型的指针 */
double *dp; /* 一个 double 型的指针 */
float *fp; /* 一个浮点型的指针 */
char *ch; /* 一个字符型的指针 */
Pointer variable ip, dp, fp, ch a generally four-byte memory space, all the pointer values of actual data type, is a long hexadecimal memory address representative.
Running on the stack pointer is smaller than the actual memory occupied by variable running, into the stack pointer can save space when large transmission parameters can save the time spent, it can be faster, and stack space is very small precious, it is impossible to go in big data compression.
Third, the reference
3.1 What is a reference
A variable reference is an alias, the equivalent of the second name of a variable
Operation references, it is equivalent to operating variables
Quote form
- Defined with reference to & boot: the reference name & type = variable name;
For example: LOL Heroes Union do not name
the type reference name = & variable name;
Game & LOL = League;
3.2 A simple example cited
Demonstrated: i is an alias ir, ir is equivalent to the operation of the operation of the i
#include<isotream.h>
void main()
{
int i =9;
int &ir =i;
cout<<"i="<<i<<" "<<"ir="<<ir<<endl;//i=9 ir=9
ir=20;//改变ir的值
cout<<"i="<<i<<" "<<"ir="<<ir<<endl;//i=20 ir=20
cout<<"i的地址是:"<<&i<<endl;//i的地址是:0x0012FF34
cout<<"ir的地址是:"<<&ir<<endl;//ir的地址是:0x0012FF34
}
Graphic codes
Ir operation is equivalent to the operation of the i
3.3 Note the use of reference points
- (1) can not be established references cited
- (2) can not be established array of references, array references can not be established
- (3) a pointer reference can be established, can not be established is a pointer to the
Code Description
int i=0,a[10];
int &&ii=i; //错误,ii是引用的引用
int &&aa=a; //错误,aa是数组的引用
int &*ip=i; //错误,ip是指向引用的指针
int *pi=&i;
int *&pr=pi; //正确,pr是指针的引用
Fourth, the difference between pointers and references
Why C ++ 4.1 to introduce references
In fact, in essence, it is to add a reference pointer constraints, improve the deficiencies cited pointer
Deficiencies pointer at 4.2
- Operation null pointer: a pointer is assigned to the 0
- Operation wild pointer: a pointer uninitialized
- Unwittingly changed the value of the pointer, and then I thought the normal pointer .
4.3 reference pointer improve
- Reference must be initialized (to ensure that no wild pointer)
- Initialization is the alias for an existing variable (to ensure that not a null pointer)
- A reference to always point to the object of his initialized (guarantee pointer value unchanged).