C + + pointer and address method _c language

Source: Internet
Author: User

First look at the following program:

Copy Code code as follows:

void Main ()
{
int a = 100;
int *ap = &a;
printf ("%p\n", &a);//output: 002af744
printf ("%p\n", AP);//output: 002af744
printf ("%d\n", *ap);//output: 100
printf ("%p\n", &ap);//output: 002af738
printf ("%p\n", &*ap);//output: 002af744
scanf ("%d");
}


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

The output of this sentence is the address of variable a, no doubt.

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

This is the value of the output pointer, which is the same as one output, that is, the value of the pointer is the address of the variable to which the pointer points

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

With a * number in front of the pointer variable, the AP pointer without an asterisk is the address of the variable a, and the * really becomes the content of the variable A that the pointer ap points to.

So, we can understand that the * number is an operation that gets the contents of the address that the pointer variable points to.

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

This sentence (same 1) is the address of the AP that gets the pointer variable

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

According to the 3rd analysis, *ap points to the content of variable A, and &*ap is the address of the content of variable A, that is the address of variable A, so the output is the same (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.