Complete the C language pointer sixth article

Source: Internet
Author: User
Tags variables value of pi

A pointer to another pointer

First, the concept of the needle:

As early as in the second part of this series, I have expounded the essence of the pointer. Today we are going to learn a pointer to the address of another pointer. Let's look at the concept of pointers first!

When we program the following declaration variables:

short int i;

Char A;

short int * PI;

The program opens up space for variables in an address space on memory, as shown in the following illustration.

Memory address →6 7 8 9 10 11 12 13 14 15

------------------- ------------------------------------------------------------------

... |  |  |  |  |  |  |  |  | |

--------------------------------------- ----------------------------------------------

|short int i |char a| |short int * pi|

As you can see in the figure:

The i variable occupies two bytes in the location of memory address 5.

The A variable occupies a byte at the location of memory address 7.

The pi variable occupies two bytes at the location of memory address 9. (Note: Pi is the pointer, my pointer here is only two bytes wide, 32-bit system is four bytes)

Next, assign the following values:

i=50;

pi=&i;

After the two sentences are assigned, the memory image of the variable is as follows:

Memory address →6 7 8 9 10 11 12 13 14 15

----- ---------------------------------------------------------------------------------

... |  50 |  |    |  6 |  |  | |

----------- ---------------------------------------------------------------------------

|short int i |char a| |short int * pi|

See no: The value of the short integer pointer variable pi is 6, which is the memory start address of the I variable. So, when we read and write to *pi, it is actually read and write to the I variable. Such as:

*pi=5//is equivalent to i=5;

You can look back at the second article in this series, where there are more detailed explanations.

Second, the pointer's address and a pointer to another pointer address

In the previous section, we saw that the pointer variable itself is also in a memory address like any other variable, such as PI's memory start address is 10. Similarly, we may have a pointer to this address.

Look at the following code:

Short int * * PPI;//This is a pointer to the pointer, note that there are two *

ppi=π

The first sentence: the short int * * ppi;--declares a pointer variable PPI, which is used to store (or point to) the address of a short int * Type pointer variable.

The second sentence: &pi that is to take PI address, ppi=π is the PI address assigned to the PPI. The address value 10 is assigned to PPI. The following figure:

Memory address →6 7 8 9 10 11 12 13 14 15

------------------------------------------------------------------------ ------------

... |  50 |  |  |  6 |   10 | |

---------------------------------------------------------------------------------- --

|short int I|char a| |short int * Pi|short int * * ppi|

As you see from the diagram, the content of the pointer variable PPI is the starting address of the pointer variable pi. So......

How much is the PPI value? --10.

What is the value of *ppi? --6, which is the value of pi.

What is the value of **ppi? --50, which is the value of I, is also the value of *PI.

Oh! Needless to say too much, I believe you should understand this kind of pointer!

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.