C + + (suspect 1) arrays and pointers

Source: Internet
Author: User

Pointer:

Many people know that pointers are the address of a stored variable, and sometimes it's quite confusing to think about it.

Array:

The array name is actually an address that points to the first address of the array.


Question 1 So how do I use pointers to manipulate arrays?

int main () {int Arr[3]={3,5,9};int *ptr1[3] = {nullptr};int (*PTR2) [3] = &ARR;//OK*PTR1 = &ARR[0];//OK//*PTR1 = &am p;arr//errorfor (int i=0; i<3;i++) {cout<< (*PTR2) [i]<<endl;//okcout<<ptr2[i]<<endl;cout << (*PTR1) [I]<<endl;//okcout<<ptr1[i]<<endl;} printf ("%d", sizeof (short)); System ("pause"); return 0;}


If you still have questions:

So, array pointers, pointers arrays look at the instructions below.

int *arry[10];       pointer array int (*ARRYP) [ten];    An array pointer, since the array pointer points to an array, and the array can be seen as a pointer, the array pointer can be interpreted as a level two pointer int a[10];             Arryp = &a;          Sets the array of address number of pointer arry[0] = *ARRYP;    Arry[0] is a first-class pointer, to assign an array pointer to an array of pointers, the content should be taken, the actual is arry[0] = A;

Tip: Array allocation is a continuous address, so

*PTR1 = &arr[0];//ok
You can also remove the values consecutively.

Second-level pointers:

The pointer is also an object, and the object has its own address, and the pointer can also assign a value to the pointer address.

int  P1 = *p2 = &p1; int **p3 = &p2;cout<<p1<<endl;cout<<*p2<<endl;cout<< **p3<<endl;

The diagram is as follows:


P3--------------------------->p2----------------------------->p1 (100)


Reference:

The reference itself is not an object, then there is no address, and there is no pointer to the reference.

It also shows that there is a reference to the pointer.


C + + (suspect 1) arrays and pointers

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.