<> Pointer access to array elements

Pointer operation itself is suitable for continuous data processing , The array just meets this requirement . So this section looks at how to use pointers to access array elements .
Let's start with an example
int a[10],*p;// Define an array and a pointer variable p p=a;// perhaps p=&a[0]; Both of these sentences mean to set an array a The first address of is assigned to p
<> Equivalent form

*p namely a[0], Is the addressing operator , That is to find out p The contents of the address stored in the variable , That is array a The first element of .
In the same way (p+i) That is to find the second in the corresponding array i Elements a[i].
meanwhile *(a+i) and p[i] They all mean the same thing .
To sum up *( Pointer +i) = *( Array name +i) = Array name [i] = Pointer variable name [i]
Array name a Is the first address of the array , It's a constant , So we can't add and subtract by ourselves .

<> Pointer array

Each element of an array is a pointer variable , This array is the pointer array .
Grammatical form
data type * Array name [ Subscript expression ];
The subscript expression indicates the number of array elements , The data type represents the type of the pointer , The array name is the name of the pointer array and the first address of the array .
Example output unit matrix with pointer array
#include<iostream> using namespace std; int main() { int line1[] = { 1,0,0 };
int line2[] = { 0,1,0 }; int line3[] = { 0,0,1 }; int* pline[3] = { line1,line2,
line3}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cout << pline[
i][j] << " "; cout << endl; } return 0; }
Take the array names of three one-dimensional arrays as the elements of the pointer array , The initialization of pointer array is completed , Each element points to a row of the matrix .

pline[i][j] It's not a two-dimensional array, it's a one-dimensional pointer array , ahead pline[i] Is the pointer name , hinder [j] Is the corresponding element .( See the above equivalent way ). The storage method in memory is also different. The elements of two-dimensional array are continuous line by line in memory , The pointer array is continuous in memory , But there is no relationship between rows .
Although there are essential differences between two-dimensional array and pointer array , But you can still access the two-dimensional array through the pointer array .
example Two dimensional array example
#include <iostream> using namespace std; int main() { int array2[3][3]= { { 11,
12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }; for(int i = 0; i < 3; i++) { for(int
j= 0; j < 3; j++) cout << *(*(array2 + i) + j) << " "; // Output two-dimensional array one by one i Row element value cout <<
endl; } return 0; }
Two dimensional array elements can be output through the address of array elements , use *(*(arry2+i)+j) This is it. arry2 The second part of an array i Line number j Column element , The corresponding subscript is arry2[i][j].