Depth Analysis: cursor / pointer pointer

Further understand the role of the pointer by a program.

On the code:

 1 #include <stdio.h>
 2 
 3 void find(char array[], char search, char** pa)
 4 {
 5     int i;
 6     for(i=0;*(array+i)!='\0';i++)
 7     {
 8         if(array[i]==search)
 9         {
10             *pa = array+i;
11             **pa = (i+1);
12             break;
13         }
14         
15     }
16 }
17 
18 int main( void )
19 {
20     char ch[50];
21     char c;
22     char* p=NULL;
23     while(1)
24     {
25         int choice;
26         printf("input the string and the key word to be found : \n\n");
27         scanf("%s %c",ch,&c);
28         find(ch,c,&p);
29         if(p==NULL)
30         {
31            printf("can't find the key word in the string \n\n");
32         }
33         else
34         {
35            printf("find the first key word in the positon of  %d \n\n",*p);
36         }
37         printf("Do you want to exit? input 0 for exit or continue\n");
38         scanf("%d",&choice);
39         if(choice==0)
40             break;
41         else
42             continue;
43         
44         
45     }
46     return 0;
47 }

The function of this program is the first character in the string matches the given character meets, and returns the position they are in the string. Such as string abcdeabdc, given character d, the first position is a match for 4 d.

Analysis: First, to understand the role of the pointer, the address pointer has its own memory, its memory spaces corresponding to the memory address value is directed. for example,

char* p ;

char * pi;

Char ** p;

pi = p; // pi points to the memory address pointed to by p, i.e., points pi and p is the same memory address, the value of pi and p

    // * p is the value of p at the memory address stored

pa = & p; // pa p points to memory address

    // * pa pa is the memory pointed to by the stored address values, i.e. the value of the pointer p is equivalent to pa ** * p, p is the value at the memory address stored

Through the above resolution, it should be more clear.

Reproduced in: https: //www.cnblogs.com/sjlove/archive/2013/04/19/3029808.html

Guess you like

Origin blog.csdn.net/weixin_34161064/article/details/94019483