What does &p, *p, p, * (&p) mean?

Source: Internet
Author: User

int *p;

P=new Int[3];

P[0]=1;

p[1]=2;

p[2]=3;

&p, *p, p, * (&P) What does each of these three mean?

Here, new int[3] Returns a pointer to the exact type of object requested, which is exactly the address of the first element of the array, that is, the first address of the value.

So &p is the address of the pointer p, which is the address of the pointer to the first address of the array.

*p: The * is used for the indirect reference of pointers (the third use of *, the other two usages are: multiplication and defining pointers). When you indirectly reference a pointer, you get the variable content pointed to by the pointer. Here is the first element of the array to get the contents, that is, 1;

P: Is the first address of the array.

* (&P): Gets the contents of the pointer p address, which is the content that points to the first address of the array.

A draft was drawn at the fingertips. A little ugly, it will look.

The code is as follows:

#include <iostream>using namespace Std;int main () {int *p;p=new int[3];p [0]=1;p[1]=2;p[2]=3;cout<<&p <<endl; Gets the address of the pointer p, which is the address of the pointer to the first address of the array cout<<* (&P) <<endl;//Gets the contents of the pointer p address, that is, the contents of the first address of the array. cout<<*p<<endl;//gets the contents of the address that the pointer points to cout<<p<<endl; Gets the contents of the pointer p stored for (int i=0;i<3;i++) {cout<<&p[i]<< "\ t" <<P[I]<<ENDL;}

Operation Result:

Personal opinion, wrong to welcome correct.

What does &p, *p, p, * (&p) mean?

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.