C ++ pointer notes and pointer notes

Source: Internet
Author: User

C ++ pointer notes and pointer notes

What is a pointer:

A pointer is a special variable. The value stored in it is interpreted as an address in the memory. With pointers, we can directly operate on the memory.

What is an address:

For example, if you want to send a parcel to your friend, do you need to fill in the address when sending the parcel, And you can deliver the parcel to the friend through the address. Similarly, for example, you define a variable as I.

The variable I exists in the computer memory, that is, the ID of I in the memory, that is, the address.

Example:

# Include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int a; // defines an integer variable aint * p; // defines an integer pointer p. * indicates that the variable P is a pointer p = &; // take out the address of variable a and assign it to Variable P. The memory address stored by P is the address of a and the symbol is the cout. <"variable a address: "<& a <endl; cout <" pointer p address: "<p <endl; cin. get (); return 0 ;}

Illustration:

We can find that the addresses to be pointed to are the same.

 

 

Null Pointer! :

A pointer is a variable used to save the memory address. If we do not initialize the pointer, It is a out-of-control pointer, which can point to any address in the memory. So remember to initialize.

Normally, the initialization value is 0.

Int * p; p = 0; // 0/* the next sentence is written in the previous merge statement. Generally, the next sentence is written in the same way. */int * p = 0; // The value is initialized to 0.

Use pointers to access stored values: 

# Include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {int love = 520; // define the love initialization value of an int type variable as 520int * gril = 0; // define the gril initialization value of an int pointer as 0 gril = & love; // use the pointer variable gril to save the address cout of the variable love <"gril value:" <* gril <endl; // The output is 520
Cin. get (); return 0 ;}

Cout <"gril value:" <* gril <endl;

Here * gril stores the address of the love variable and outputs 520

 

Notes that are prone to errors:

For pointers, the most confusing concept is: pointer memory address and pointer address value !!!

I'm a little dizzy with my head, so now I have to take notes .....

 

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.