C/C ++ pointer and address fetch operations

Source: Internet
Author: User

First look at the following program:

Void main () {int a = 100; int * ap = & a; printf ("% p \ n", & a); // output: 002AF744printf ("% p \ n", ap); // output: 002AF744printf ("% d \ n", * ap); // output: 100 printf ("% p \ n", & ap); // output: 002AF738printf ("% p \ n", & * ap); // output: 002AF744scanf ("% d ");}

1. printf ("% d \ n", & a); // output: 002AF744

This sentence outputs the address of variable a, which is beyond doubt.

2. printf ("% d \ n", ap); // output: 002AF744

This sentence is the value of the output pointer, which is the same as an output, that is, the value of the pointer is the address of the variable pointed to by the pointer.

3. printf ("% d \ n", * ap); // output: 100

A * sign is added before the pointer variable. The ap pointer without the star number points to the address of variable, the addition of * actually becomes the content of the variable a pointed to by the ap,

Therefore, we can understand that * is an operation to get the content of the address pointed to by the pointer variable.

4. printf ("% d \ n", & ap); // output: 002AF738

This statement (same as 1) is the address for obtaining the ap pointer variable.

5. printf ("% d \ n", & * ap); // output: 002AF744

According to the analysis at, * ap points to the content of variable a, and & * ap is the address for obtaining the content of variable, is the address of variable a, so the output content is the same as (1)

 

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.