Since pointer variables can point to variables, they can also point to array elements (placing an element's address in a pointer variable), and the so-called array element's pointer is the address of the array element.
int a[10];
int *p;
p=&a[0]; Assign the address of the a[0] element to the pointer variable p, which means p points to the No. 0 element of array A.
P=a; Equivalent to the previous sentence, the C language specifies that the array name represents the address of the first element in the array.
Attention:
(1) P+i and A+i is the address of a[i], that is, the actual address is calculated as a+i*d, because of the int type, so d=2.
(2) * (P+i) or * (a+i) is the element pointed to by p+i or a+i, i.e. a[i]
(3) Pointer variables to an array can also be labeled, such as P[i] and * (p+i) equivalent
So, by referencing an element, you can use the
1, subscript method, such as A[i]
2, pointer method, such as * (A+i) or * (P+i)
There are several issues to note when using pointer variables to point to array elements:
(1) You can point to different elements by changing the value of the pointer variable, eg:p++, this is possible.
(2) Note the current value of the pointer variable
(3) The size of the array in the above example is 10, but actually the pointer variable p can point to the memory unit after the array.
(4) Note the operation of the pointer variable (P=A)
I, p++, causes p to point to the next element, i.e. a[1], and if *p is executed, the value of a[1] is obtained.
II, *p++, because * and + + is the same priority, the binding direction is from right to left, so equivalent to * (p++), the role is to get *p first, and then make p+1
Pointers and Arrays