Pointers often haunt us in C programming, but the flexibility to use pointers will make programming easier and more efficient. Here's the difference between *p[n], (*p) [N], and **p, and this is where I've often been troubled.
The definitions of these three are:
int *p[n] represents an array of pointers, which means that N different pointers to int types are defined.
an int (*p) [N] represents a pointer that defines a pointer, pointing to a int[n] type.
An int **p represents a pointer that defines a pointer.
Note that the priority of [] is higher than *.
The pointer represents an address that holds a variable of a data type, such as:
#include <stdio.h> #include <math.h> int Main ( int *p, I; int a[3 ]; P = A; for (I=0 ; I<3 ; I++) a[i] = I; printf ( " %d\t%d\n " , * (P+1 ), A[1 Span style= "color: #000000"]);
return 1;}
Here P Stores the first address of the array A, and is of type int, then p+1 represents the address of the address, that is, a[1], where p moves backwards by a byte of type int.
The above result is:
1 1
Here are some examples to illustrate the differences between these pointers *p[n], *p[n] and **p.
#include <stdio.h>intMainintargcChar*argv[]) { intI, J; int(*P1) [3], *p2[3], **P3; intb[3][3]; for(i=0; i<3; i++ ) for(j=0; j<3; J + +) B[i][j]=2*i +J; P1=b; for(i=0; i<3; i++) P2[i]=B[i]; P3=malloc(3*sizeof(*p3)); for(i=0; i<3; i++) * (p3+i) =B[i]; printf ("The matrix is: \ n"); for(i=0; i<3; i++) { for(j=0; j<3; J + +) printf ("%d\t", B[i][j]); printf ("\ n"); } printf ("%d\t%d\t%d\n", * (* (p1+1)+1), * (p2[1]+1), * (* (p3+1)+1));}
Explanatory notes:
*p[3] represents an array of pointers, which can be understood as defining three pointers *p1[0], *p1[1], *p[2], where the address of b[0] is assigned to P[0],B[1] in this example (P[2]),..., so * (the "*") means * (p2[1]+1) +1) value, which is b[1][1].
**p represents a pointer to a pointer, or it can be understood that P stores the address of an address. For example, in this example, {b[0], b[1], b[2]} can be considered an array, the element inside is an address, p stores the initial address of the array, that is, the first element of the array is *p (* (p+1) represents the second element of the array), But notice that this first element is still an address, its equivalent array b[0] 's first address, so * (* (p3+1) +1) equivalent to * (B[1]+1), that is b[1][1]
(*p) [3] The priority of the natural operator is changed, equivalent to (int) ((*p) [3]). A headache with a pointer, we replace the pointer first, that is, int a[n], is an n-dimensional array, the first address of the array (that is, the array name) is a. So, int (*p) [n] is also an n-dimensional array, but the first address of this array is *p, that is, the content p points to is the first address of an array. So, p is a pointer to an array, and the elements in this array are int. In fact, P3 is equivalent to a double pointer, which is almost the same as **p.
The above is my personal understanding of the pointer, if there are errors, I hope that the majority of netizens pointed out that
Reference post: http://blog.csdn.net/ywb201314/article/details/52062059
C language Pointer *p[n], (*p) [N], and **p difference