I also want to learn C language-Chapter 2: pointer (1)

Source: Internet
Author: User

What is the pointer?
Pointer is an abstraction. in a computer, you can specify an address number. Of course, you can carefully and effectively abstract everything in your life, A pointer in a computer is equivalent to an address number + its explanation of this address number. A pointer variable is a codename. You can use this codename to obtain a pointer. Why is there a variable generated? It is mainly used to prevent programmers from directly submitting the variable with the address. The pointer variable also occupies memory, so the pointer variable will have its address. Since there is an address, this address can be assigned to another pointer variable, the other pointer variable is called the pointer to the pointer.

Direct and indirect reference of Variables
The variable name is called direct reference, and the reference to the variable through the pointer is called indirect reference.

Two indirect references
1. If the address of a target variable is stored in a pointer variable, it is called a level-1 address.

2. If a pointer variable contains the address of the pointer variable pointing to the address of the target variable, this is called the second-level address.

How can we understand these two sentences. You like one person, find its number (pointer) in the phone book, and then you can call to confess it! This is the first-level address. If you like one person, but you don't have his phone number, but you know his sister's phone number, you can take out a phone book to query her sister's phone number (pointer) then she finds the person you like through her sister's phone book (pointer! That is to say, the second-level pointer gets the first-level pointer when getting the content, and the second-level pointer gets the target variable!

Note that if a program pointer exceeds 2 levels, you should consider whether your design is reasonable.

Example of a pointer to a pointer:

Void main () {int a = 99; int * pa = & a; int ** ppa = & ;}

Do you think this program is correct? When you are careful, you will immediately respond. This is absolutely impossible, because the type does not match! An error will be reported!

Then let's look at this program:

Void main () {int a = 99; int * pa = & a; int ** ppa = & ;}

Hey! That's all right! Actually! This write is also incorrect, because the address to get the variable is an address constant, and the address constant cannot get the address! We can only get address characters for things with memory space!

So the correct one should be like this:

Void main () {int a = 99; int * pa = & a; int ** ppa = & pa ;}

Next let's look at a program. This program understands what the second-level pointer is:

Now let's look at one row:

The first printf statement, a, is known to all. The output must be 63, because the hexadecimal value is 63 in decimal 99!

The second printf statement pa. Does pa represent 0x0012ff40! The essence of a variable is to avoid programmers and addresses from being handed in! 04 is 0012ff44! Right!

The third printf statement * pa, * pa is the content of 0012ff44 In the second sentence, of course 63!

The fourth printf statement is ppa. What about ppa! You can see from the previous notes that the displayed value is 0012ff40!

The fifth printf statement, * ppa, * ppa! Alas! It seems so simple now, it shows ff44

The sixth printf statement, ** ppa, ** ppa, is the content of 0012ff44! 63!

Is this analysis easy! Never dizzy! Haha!

This note is recorded here first. In (2), we continue to record the application of the pointer pointing to the pointer. I have been studying for six consecutive hours. I am tired. I want to go out! Happy walking! Hey! Because what do I think is the pointer of a pointer .... It will all be a float cloud!

 

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.