Pointer (pointers to pointers)

Source: Internet
Author: User

Generally, pointers are the addresses in the memory. Different types of pointers represent the addresses of corresponding types of variables. The pointer itself also needs an address, so we can clearly define the pointer. The following is

Pointer and a pointer pointing to an integer pointer:

Int I = 1; <br/> int * Pi = & I; <br/> int ** Pi = & Pi

 

 

The values of each variable are output as follows:

 

I = 1 * Pi = 1 ** PPI = 1 <br/> & I = 0012ff60 Pi = 0012ff60 * PPI = 0012ff60 <br/> & Pi = 0012ff54 PPI = 0012ff54 <br /> & PPI = 0012ff48 <br/>

 

 

* PPI = 0012ff60 <br/> & (** ppi) = 0012ff60 <br/> * (& (* ppi )) = 0012ff60 <br/> * (& ppi) = 0012ff60

 

Because the address of the first element of the array name, the address pointer can also represent the array. A pointer can represent a two-dimensional array. A string pointer can be a string, and a pointer to a string can be a string set.

Determine the output of the following program:

 

Int main (INT argc, char * argv []) <br/>{< br/> const char * P = "12345 "; // P points to the first letter of string "12345" <br/> const char ** q = & P; // Q points to P, Q = & P <br/> * q = "ABCDE"; // means P points the first letter of "ABCDE" <br/> const char * s = ++ P; // s = p + 1; so * s = 'B' <br/> P = "xyzwvu "; // P points to the first letter of "xyzwvu" <br/> cout <* (++ s) <Endl; // ++ S; so * s = 'C' <br/>}

 

Note that in the third sentence of the program, the object pointed to by the pointer P is modified through its own pointer Q, so the final output is C.

 

In addition to the above, we can find that the stack in the memory increases in the direction of address reduction (why is it 12 bytes each time ?), You can also see that the values in each row are equal. PPI is a pointer to the integer pointer Pi, so PPI = & PI; Pi is an integer pointer to the integer variable I, so pi = & I.

 

Here we can see that the & (address operator, address-of operator) and * (dereference operator, dereference oprator) are inverse operations. As follows:

 

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.