1. array pointer to a one-dimensional array
An array pointer is a pointer to an array. For example
int (*p)[10];
P is a pointer pointing to a one-dimensional array containing 10 elements, and the array element is an integer.
If int * P [10] is written, it is a pointer array. Because [] has a higher priority than *, P is first combined with []. P is the data of the array type, the element is a pointer.
The following is an experiment on getting an address from an array pointer and a one-dimensional array name:
#include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,0}; int (*p)[10]; p=&a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; system("pause"); }
The address value is the same, but the meaning is different.
1) A is the array name and the first address of the array. It is equivalent to & A [0] and the type is int *, that is, the pointer to the int. Is a one-dimensional pointer, moving an int every 1 more. For example, int * P = A; P ++; P points to element a [1]. The experiment code is as follows:
#include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,0}; //int (*p)[10]; int *p=a; p++; cout<<*(p)<<endl; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; system("pause"); }
Result:
2) The type of & A is int * [10], and the type is a pointer to an array containing 10 int elements. It is a second-level pointer. Every increase of 1 moves 1 int [10] units. For example, * (& A + 1) indicates a unit after the entire array, that is, a unit after 0. * (& A + 1)-1) indicates a [9] = 0. The experiment code and result are as follows:
#include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,0}; int (*p)[10]; p=&a; cout<<*(*(&a+1)-1)<<endl; cout<<*(*(p+1)-2)<<endl; system("pause"); }
Result:
3) P is an array pointer pointing to a pointer A to an array containing 10 int elements. Consistent with &.
2. An array pointer pointing to a multi-dimensional array.
The pointer has no relationship with the dimension of the array to which it points. The key is that the array pointer is a second-level pointer pointing to a one-dimensional array unit. The array pointer moves a unit, which is equivalent to moving an entire one-dimensional array.
To better understand the differences between array pointers and common pointers and second-level pointers, the following is an example.
For example:
Int A [3] [3];
INT (* P) [3] =;
Here, A is an array name of a two-dimensional array, which is equivalent to a second-level pointer constant. P is a pointer variable that points to a one-dimensional array containing five int elements,In this case, the increment of P is in the unit of the length of the one-dimensional array.P + I is the starting address of line I of two-dimensional array A. * (p + 2) + 2 indicates the element address of two columns in two rows of array A (the first row is 0, the first column is column 0), and * (p + 2) + 2) indicates the value of a [2] [2.
The experiment code is as follows:
# Include <iostream> using namespace STD; int main () {int A [3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int (* P) [3]; // the pointer has no relationship with the dimension of the array to which it points. The key is that the array pointer is a second-level pointer pointing to a one-dimensional array unit. The array pointer moves a unit, which is equivalent to moving an entire one-dimensional array. P = A; // P = & A; cout <* (a + 1)-1) <Endl; // compare one-dimensional cout <* (p + 1)-2) <Endl; // compare one-dimensional cout <"* (* A + 2) + 2): "<* (a + 2) + 2) <Endl; cout <" A [2] [2]: "<A [2] [2] <Endl; cout <" P [2] [2]: "<p [2] [2] <Endl; cout <"* (p [2] + 2):" <* (p [2] + 2) <Endl; System ("pause ");}
Result: