(1) pointer array: array, but each element in the array is a pointer
int * P [5]; // For example, P [2] is a pointer, * P [2] = 3;
(2) pointer to an array: A pointer, But it points to an array
int A [5];
int (* P) [5]; // compared with the previous row, * P is equivalent to a, that is, P = & A; like: int m; int * PM; // * PM is equivalent to M. PM = & M;
P = & A; // It can be merged with the previous row into int (* P) [5] = &;
----------------------
A represents the array. It is a pointer pointing to the first element.
remember that A is the array name, & A indicates that the address of the variable A is not used, instead, the address of the array element
---------------------------------
the type of A is int [5] array
& A is the INT (*) [5] pointer. -- Pointer to the int [5] array
& A [0] type is int * Pointer -- pointer to the int type.
Sizeof (A) = 20;
Sizeof (* A) = 4; because there is a value *, it means to regard a as a pointer (int *), and a points to the first address of the array,
That is, a = & (A [0]), that is, sizeof (* A) = sizeof (* & (A [0]) = sizeof (A [0]) = sizeof (INT) = 4.
Sizeof (* & A) = 20; // because P = & A, so = sizeof (* P), and * P = A, so = sizeof (A) = 20;
---------------------------------------
Int A [5] = {1, 2, 3, 4, 5 };
Int * PTR = (int *) (& A + 1 );
Printf ("% d, % d", * (a + 1), * (ptr-1); // output:
Adding a pointer to 1 should add a certain value according to the pointer type. for different types of pointers + 1, the size increases. The pointer is only a memory address, but the length of the pointer to the address may be different, for example, char * PC and int * Pi, sizeof (PC) = sizeof (PI) = 4, but why is cout output <* PC only takes one character from memory, cout <* PI takes four characters from the memory, which is determined by the pointer type (char, INT.For a * P; p + 1 = (A *) (the address value of P + sizeof (A), such as PC + 1 = PC + sizeof (char) = (char *) (PC address value + 1 byte), while PI + 1 = PI + sizeof (INT) = (int *) (PI address value + 4 bytes ).
PairCodeIn & A + 1, & A is an array pointer and its type is int (*) [5], because P = & A, that is, p type. so & A + 1 = & A + sizeof (a), where A is int [5] :( replace a = int [5] into a * P, it is equivalent to int (* P) [5]). therefore, the address value of & A + 1 = & A + 5*4 bytes is changed to the next address of the end address of array A (that is, & A [5]), & A + 1 is still int (*) [5] type. It is forcibly converted to int * by (int *) (& A + 1) and assigned to PTR. the following ptr-1 = PTR-sizeof (INT) = PTR address value-4 bytes, that is, the address of the last element of array A & A [4], * (ptr-1) it is a [4], so the output is 5.
A + 1 = a + sizeof (INT) :( here a degrades to int *, so for a * P, A is int) = the address value of A + 4 bytes, if it is & A [1], * (a + 1) is a [1], so Output 2.
Another example:
Double T [] = {1, 2,578,111, 90 };
Double * PT = & T [3]; // The point value is 111.
Int * ptint = reinterpret_cast <int *> (PT );
Char * ptch = reinterpret_cast <char *> (PT );
Cout <* (Pt-1) <"\ t" <* (reinterpret_cast <double *> (ptInt-2) <"\ t" <
* (Reinterpret_cast <double *> (ptCh-8); // The final output result is 578
---------------------
Void fun (int * P) is the same as void fun (int p []), that is, the array in the function list degrades to a pointer.