* P used in two cases:
1. The definition of a pointer variable, such as char * p; where p is a variable, only at this point with no difference int a; p, but in particular the variable which can store address.
Extension: For char ** p, p stored in an address add1, add1 add2 storage area corresponding to the storage, and the storage area corresponding to the address add2 before storing real Data; shown in Figure 1:
2. * p, p is used to fetch the data corresponding to the address stored in the storage area, as shown in Figure 2.
For example: There are main function char * p, p =. . . Let p be stored in a first memory address. In this case to call a subroutine, if you want to modify the memory pointed to by p Functions in fun, it is necessary to get the address of p, i.e., fun (& p), then * p =. . . P can be diverted at the memory, specifically as shown below:
void FUNC ( char ** P2) { char * P1; P1 = ( char *) the malloc ( the sizeof ( char ) * . 8 ); P1 = " Hello " ; * P2 = P1; } int main () { char * P = NULL; FUNC ( & p); the printf ( " p S =% \ n- " , p); // Print p points to the memory stored string. getchar (); return 0 ; } Output: hello
A typical error:
See if you can find out:
. 1 void T ( char ** P2) { 2 char * P1; . 3 P1 = ( char *) the malloc ( . 1 ); . 4 * P2 = P1; . 5 } . 6 int main () { . 7 char ** P; . 8 . 9 T (p); 10 return 0 ; . 11 } Why is it suggested to run a p not initialized.
Look at the example of a ** p:
void t(char **p2) { char *p1 ; //p1 = (char *)malloc(1); p1 = "hello"; *p2 = p1; } int main(){ char **p; p = (char **)malloc(sizeof(char) * 8); char arr[] = "zhang"; *p = arr; printf("a[0]=%c\n",arr[0]); t(p); printf("*p=%s,p=%d\n",*p,p); printf("a[0]=%c\n",arr[0]); return 0; }
Output:
a[0]=z
*p=hello,p=13531344
a[0]=z
In this way the original arr [] data is still there, but does not point to the * p, * p points to a "hello" where the memory address.
Look at one of:
void T ( char ** P2) { char * P1; P1 = " Hello " ; * P2 = P1; } int main () { char ** P; // P = (char **) the malloc (the sizeof (char) 8 *); char arr [] = " zhang " ; the p- = ( char **) arr; // * = arr think the p-annotation difference in this way? the printf ( " A [0] = C% \ n- " , ARR [ 0 ]); T (P); the printf (" P =% S, P =% X \ n- " , * P, P); the printf ( " A [0] =% C \ n- " , ARR [ 0 ]); return 0 ; } Output: The original array is washed out of A [ 0 ] = Z P = Hello, P = 75fe70 A [ 0 ] =?