Relationship between one-dimensional arrays and pointers in C + + summary _c language

Source: Internet
Author: User

for array int a[10];
A represents the address of the first element of the array, that is, the &a[0];

If you make the pointer p, point to the first element of the array, you can do so:
int * P=A;
Or
int *p=&a[0];

So p++, is to point to the first element in the array, that is a[1];
At this point *p is the value that is placed in a[1].
At this point, a[i]=p[i]=* (a+i) =* (p+i)

Let me give you an example;
Directly using A[i] to output

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
for (i=0;i<10;i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}

use * (a+i) to output
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
for (i=0;i<10;i++)
cout<<* (a+i) << "";
cout<<endl;
return 0;
}

use * (p+i) to output
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
for (i=0;i<10;i++)
cout<<* (p+i) << "";
cout<<endl;
return 0;
}

About *p++
Because + + and * have the same priority, the binding direction is from right and left, so it is equivalent to * (p++). The effect is to get the value of the variable that p points to (that is, *p), and then add the value of P to 1.
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
while (P<A+10) {
cout<<*p++<< "T";
}
cout<<endl;
return 0;
}

equivalent to
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
while (P<A+10) {
cout<<*p<< "T";
p++;
}
cout<<endl;
return 0;
}

*p++ is equivalent to * (p++), and * (++p) means first p+1, then *p.
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
while (P<A+10) {
cout<<* (++p) << "T";
}
cout<<endl;
return 0;
}

Run the above program, the result will output the value of a[2] to a[11], where a[11] is not defined.

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.