The significance of *,& in pointer operation
(1) *
We all know that when you write int *p, you can declare a pointer. Few people know that there is a name of "dereference" in C + +. What he means is to explain the reference, and to say the popular point is to look directly at the contents of the address that the pointer refers to, which can be any data type, or it can be a pointer (this is the double pointer, which will be discussed later). It is important to note that when declaring a variable, the * cannot be used as a dereference, just that the variable you declare is a pointer type.
Example1:
int a=50;
The function of int *p=&a;//' & ' is to extract the A variable in the memory address, which is explained in detail later.
*p=5;//This is the dereference, * explains P's reference to the memory address of the constant 50, explaining that the result is to go directly to the content referred to by P, because p points to a, so the content of a will be modified to 5 instead of 50.
(2) &
& refers to a reference, there are & symbols, that is, the variables within the function are the same as the variables of the main function, the function changes its value, and the corresponding variables of the main function are changed; without the & symbol, the variable inside the function is a copy of the variable of the main function, changing its value within the function. Does not change the value of a variable in the main function.
& Specific 3 semantics, bitwise AND, take address and declare a reference.
int a=0x0000002 & 0x00000003;//Bitwise AND
int *p=&a;//The address, take out a in-memory address to P, so that a pointer map is established so that P refers to a
int &a=b;//declares A is a reference to B, and a modification to a operation is a direct modification to B.
There is also a term called "de-dereference" in the pointer operation, in which the &*p (assuming P is a pointer),&,& The dereference of a, which results in an offsetting effect, and the address of the variable referred to as P.
Example2:
Connect example1.
println ("%x | | | | | %x ", &p,&*p);
Output Result:
P in-memory address | | | | | A in-memory address
Why is it?
First & Go to the address of the pointer p, and then & Remove P to a in a 5 reference, dereference and de-dereference effect offset, so that P refers to the address of the variable, that is, a in memory address.
Reference to pointers in C, dereference and de-dereference