An array is a contiguous set of data, which can be traversed by the arithmetic operation of the pointer, which in turn points to each element of the array.
Defines a pointer to an array element:
int a[], *PA; PA=&a[0]; or pa=a;
After the above definition and assignment:
*pa is a[0],* (pa+1) is a[1], ..., * (Pa+i) is a[i].
A[i], * (pa+i), * (A+i), pa[i] are equivalent.
//example 6-7 (1) using array names and subscripts to access array elements#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0}; for(inti =0; I <Ten; i++) cout<< A[i] <<" "; cout<<Endl; return 0;}//Example 6-7 (2) accessing array elements using array name and pointer operations#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0}; for(inti =0; I <Ten; i++) cout<< * (a+i) <<" "; cout<<Endl; return 0;}//Example 6-7 (3) using pointer variables to access array elements#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0}; for(int*p = A; p< (A +Ten); p++) cout<< *p <<" "; cout<<Endl; return 0;}
Array of pointers: The elements of an array are pointer-type
//example 6-8 using a pointer array to hold a matrix#include <iostream>using namespacestd;intMain () {intLine1[] = {1,0,0};//three rows of the matrix intLine2[] = {0,1,0}; intLine3[] = {0,0,1}; int*pline[3] = {Line1,line2,line3};//defines an array of integer pointers and initializescout <<"Matrix Test:"<<Endl; for(inti =0; I <3; i++){ for(intj =0; J <3; J + +) cout<< Pline[i][j] <<" "; cout<<Endl; } return 0;}
PART6 arrays, pointers and strings 6.6 pointers and arrays