Two methods of pointer manipulation array of summary

  • 2020-05-19 05:15:37
  • OfStack

Pointer to the array, method 1 is p+index, method 2 is p[index], the second method is the same as the array access method.

The array reference returns the address of the pointer to the first element of the array.

Pointer to an array of any element can be, and then from there to visit, just pay attention to not overstepping, this suggests that the array elements only the stack in a row, don't need no other configuration information stored in the array elements outside or anywhere in the end, and so on, all have no, he just for storage.


#include <iostream>
using namespace std;

int main()
{
 const int ARRAY_LEN = 5;

 int MyNumbers[ARRAY_LEN]={24,-1,365,-999,2011};

 // int * pNumbers = MyNumbers;

 // cout << "Displaying array using pointer syntax,operator*" << endl;

 // for(int Index = 0; Index < ARRAY_LEN;++Index)
 // cout << "Element " << Index << " = " << * (MyNumbers + Index) << endl;

 // cout << "Displaying array using pointer with array syntax,operator[]" << endl;

 // for(int Index = 0; Index < ARRAY_LEN;++Index)
 // cout << "Element " << Index << " = " << pNumbers[Index] << endl;





 int * pNumbers = &(MyNumbers[1]);

 cout << "Displaying array using pointer syntax,operator*" << endl;

 for(int Index = 0; Index < ARRAY_LEN-1;++Index)
 cout << "Element " << Index << " = " << * (MyNumbers + Index) << endl;

 cout << "Displaying array using pointer with array syntax,operator[]" << endl;

 for(int Index = 0; Index < ARRAY_LEN-1;++Index)
 cout << "Element " << Index << " = " << pNumbers[Index] << endl;

}

Related articles: