Pointer parsing (1) (original)

Source: Internet
Author: User

I have been thinking about this topic for a long time. I don't know how to write it down? I am shaking hands next time. You guys may laugh when you see what I wrote by cainiao, but no matter what it is! I want to write it down !!! This is indeed so important that countless people have bent on it, and countless buckets are hidden here. It is love, crazy, painful, and itchy. Some readers think that I will write "hate". To tell the truth, I have no hatred for the pointer. I admire it and worship it. If it was a girl, I immediately married it.
I think the pointer topic can be divided into the following parts:
1. Basic pointer content and application
2. Relationships and differences between pointers and Arrays
3. function pointer
4. smart pointer
Let's take a look at the above four parts, so I can understand these capabilities. You can post comments if you want to add more information. The write speed is poor. You can yell at it. I can accept it very much. I am sorry because it takes some time to read a bad article.
1. Basic pointer content and application
(1) Basic Content
Speaking of pointers, we immediately thought that this thing was related to the address. Speaking of addresses, we will think of memory. The normal thinking should be able to relate to this aspect. The computer memory is composed of many bits. These bits are either 1 or 0. Example: 100010010101110% ...... & %¥, I rely on it. You can understand it. Therefore, it is necessary to divide the byte, each byte contains eight digits, which can be expressed as: 0 ~ 255. However, this is not enough. It's too small. Therefore, a word appears, which generally consists of two bytes.
Now you may ask, it's just a waste of time. I don't know what this is for ("100010010101110% ...... & % ¥ "), Do not know how this can be achieved? First of all, frankly speaking, I don't know what it is. For example, the following code:
Int a = 100;
Float f = 2.11;
These values are composed of 0 and 1 in memory. We cannot check the type by the check bit. You must put it in the program to determine its actual usage.
The memory is exhausted. You can say the address. Everyone's house has a house number, so that we can buy things online, express delivery can be sent to our house. Similarly, we define a variable in the program, not a declaration! This variable has a place in the memory. In the memory, it has a location for it, and the organization divides it into an address. We came to find this variable through the address. Of course, it was a great deal of hard work during the period. Now I don't want to talk about the OS. What does the address look like? You can use the following code and image display:
# Include <iostream>
 
Int main ()
{
Int a = 100;
Int * pa = &;
 
Std: cout <"this is the value of a:" <a <std: endl;
Std: cout <"this is the address of a:" <pa <std: endl;
 
Return 0;
}
Running result:

 
 

See, 0x0016FCEC. This is the address in the memory of "a = 100. The specific address is in the middle, front, and tail of the memory stick. I don't know this. "0x" indicates the hexadecimal format. fcec indicates that there are a total of 32 characters, 4 bytes, and 2 characters. At this time, I found two strange symbols: "&" and "*". The following code explains:
Int a = 100;
Int * pa = & a; // * pa indicates an int pointer, and * Indicates indirect addressing or indirect reference operators.
// & Indicates the address operator for obtaining the value
:

 

 

(2) Basic Applications
① Pointer, indirect access and left Value
Run the following code:
Int a = 100; // The left value of a is 100.
Int * pa = & a; // address whose left value is
Int B = * pa-10; // The left value of B is 90. * pa indicates the resolution reference of the pa pointer, representing the value of.
It's a little strange, "* & a = 100" What does this mean? The answer should be the same as "a = 100. We analyze the following: "& a" is the address that generates a, which is a pointer constant ." * (& A) ", it should be clear ." * "Unreferenced" indicates the address expressed by the Access operand. Www.2cto.com
Next, let's take a look at the pointer. This is a bit tricky! See the following code:
# Include <iostream>
 
Int main ()
{
Int a = 100;
Int * pa = &;
Int ** ppa = & pa; // This is the pointer of the pointer. Level 2 pointer
Std: cout <"this is the value of a:" <a <std: endl;
Std: cout <"this is the value of a:" <* pa <std: endl;
Std: cout <"this is the value of a:" <** ppa <std: endl;
Std: cout <"this is the address of a:" <pa <std: endl;
Std: cout <"this is the address of pa:" <ppa <std: endl;

Return 0;
}
Running result:

 

 

② Pointer expression

It's time to start mental training!

Char ch = 'a'; char * pch = & ch;
There are several problems:

1. What does & ch represent?

2. What does pch mean?

3. What does & pch represent?

4. * What does pch mean?

5. * What does pch + 1 represent?

6. * (pch + 1) What does it mean?

7. What does ++ pch mean?

8. What does * ++ pch represent?

9. * What does pch ++ represent?

10. ++ * What does pch represent?

11. What does (* pch) ++ represent?

12. What does ++ * ++ pch represent?

13. ++ * What does pch ++ represent?

These problems come from the pointer section in "C and pointer.

③ Uninitialized, Invalid Pointer

Int * a;... * a = 12;
Do you see the above error? After you break this code into your computer, do you want to execute the following?

 

 

This is an easy mistake when we use pointers. The pointer is used without initialization. The result is .... The solution to this problem is very simple. When you use it, first define it, not declare it, and then use it.

From cloud flying elephant cg


 

Related Article

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.