Summary of the relationship between one-dimensional arrays and pointers in C ++

Source: Internet
Author: User

For the array int a [10];

A Indicates the address of the first element of the array, that is, & a [0];

If the pointer p points to the first element of the array, you can perform the following operations:

Int * p =;

Or

Int * p = & a [0];

Then p ++ points to the first element in the array, that is, a [1];

* P is the value in a [1.

In this case, a [I] = p [I] = * (a + I) = * (p + I)

The following is an example;

Output directly using a [I]

 

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

Output with * (a + I)


 

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

Output with * (p + I)


 

#include<iostream>  using namespace std; int main(){     int a[10]={1,2,3,4,5,6,7,8,9,10};     cout<<"Please input 10 intergers: "<<endl;     int i=0;     int * p=a;     for(i=0;i<10;i++)     cout<<*(p+i)<<" ";     cout<<endl;     return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 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 and the combination direction is from right to left, it is equivalent to * (p ++ ). The function is to first obtain the value of the variable pointed to by p (* p), and then add 1 to the value pointed to by p.


[

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


 

Equivalent

#include<iostream>  using namespace std; int main(){     int a[10]={1,2,3,4,5,6,7,8,9,10};     cout<<"Please input 10 intergers: "<<endl;     int i=0;     int * p=a;     while(p<a+10){         cout<<*p<<"\t";         p++;     }     cout<<endl;     return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 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 ++). * (++ p) indicates that p + 1 is used first, and * p is used.

 

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

Running the above program will output the values from 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.