C ++ pointer use

C ++ pointer use

Premise:
the computer, memory retrieval variable, the variable needs to know the physical address , the equivalent of real life landmarks , landmark when locked, when the process variables from memory to call in order to achieve accurate.
And we pointer provides a landmark values , philosophy popular to say, within a certain time landmark accurate representation of the value of a house , we can say that when we talk about landmarks value, the equivalent of talking about the house, when we say since the house is a landmark value.
Pointer value --- --- landmark house, so, Trinity, Siamese twins, you are me and I am you.
Now we look to define the pointer
pointers: is the address of the variable container as defined in (on behalf of it abstractly or variable)
pointer address --- --- variables, so the Trinity, Siamese twins, you are me and I am you.
A pointer to a coordinate value and a pointer to the address to be associated with the actual,
but rising to house and variables, required abstract equal.
Just like you and identity as

How to get the computer address of the variable?
Was added & taken a symbol representing the address of the variable

int a = 1;
std::cout << &a;``

The printed result is a string of integers,
described pointer / address is actually a series of unsigned integers.
This string is an integer cpu memory will be allocated on the physical location of the hardware random variables in the process.
And how can we control the distribution, was reasonable, just and efficient? Operating system have the final say, which pull away.

This premise is,
when we go to the address corresponding to the received type of variable, we need to create a container for storing the address corresponding to the variable type (pointer) the PS; after all be replaced with a pointer.

int a = 10;
int* p = &a;

Similarly extended to an array

int a[10];
int* q = &a;//存储数组a的首地址,也就是第一个元素地址
//c++星号写在数据类型后,代表整形指针类型
int* p = &a[5];//存储数组a第6个元素地址,数组下标从0开始

Similarly, since p represents a [5] address, abstract represents a [5],
since an array stored in a row, p + 1 is the A [. 6]
(PS: A +. 5 is equivalent to & a [5])
if before the pointer, then starred, takes a value representative of the current pointer contained in the address of the variable.
Popular point that once a pointer is added an asterisk (* p), * p is not representative of the abstract a [5] a, * p is a [5]
,
empathy * (p + 1) is a [6 ]

Function pointer variable parameter

After the above we know, the address pointer variable as a container, storage of an unsigned integer is a string, when the transfer function parameters, then passed as a value, corresponding to the copied value. A copy of the value of the pass later, I myself have not changed.
Analogy: I tell them my thoughts, others accepted my thoughts were his own creation to become his own ideas, others to change my mind, but my mind has not changed.

void change(int x){
    x = 233;//
}
int main(){
int a = 1;
change(a);
std::cout << a;
}//此时a值打印出来是1,我的思想并没有变

If you want to change a value, to be added at the time of transmission symbols & parameter, as a reference
means will alias is passed in a x, i.e. change of x for a change

void change(int &x){
    x = 233;//
}
int main(){
int a = 1;
change(a);
std::cout << a;
}//此时a值打印出来是233,a改变了

And take breaks & different address
and types together is a reference, and variables together is address-
int * & p out & reference is to represent the
next
we speak point the way
we look at & p, p & p is the address of a pointer to take , p as the address of the container only keep the address of a variable, if it should have a name, then again to set up a pointer q points to it, and that it acts as a container to store variable addresses, it also addresses a, q as a container deposit with p this unsigned integer address string, a set of a ring, a ring set ,,, endless also.
Investigate its origin is that this string unsigned integer which can function as a coordinate value somewhere, but also as a coordinate value of the coordinate values, if I called Zhang Jin, goes into a list of names should have its own label the label also has its own label ,,, label also has its own label ,,,,, if returned to his philosophical origin, who I really am, I called Zhang Jin, and Zhang who is it? ? ? ?
If you have defined it, as well as three pointers, four pointer, five pointer. . . . .

The following three examples give & prove useful as a reference symbol, then again

And together is a reference type, and variables is to take the site together

Type a:

void change(int* p){
	*p = 233;//*p等同于a
}
int main(){
int a = 1;
int* p = &a;
change(p);
std::cout << a;
}//此时a值会从1变为233

Type II:

void change(int* p){
	int b = 233;
	int* q = &b;
	*p = *q;//不加引用,需要加*p来等同于a,然后用*q(b)去替换*p才能转换,
}
int main(){
int a = 1;
int* p = &a;
change(p);
std::cout << a;
}//此时a值会从1变为233

Type III:

void change(int* &p){
	int b = 233;
	int* q = &b;
	p = q;//加了引用之后,改p的地址就相当于改a的地址
}
int main(){
int a = 1;
int* p = &a;
change(p);
std::cout << a;
}//此时a值会从1变为233
Published 19 original articles · won praise 4 · Views 501

Guess you like

Origin blog.csdn.net/qq_35050438/article/details/103213496