Reference to pointers in C, dereference and de-dereference

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.