Multi-level pointers in C Language

Source: Internet
Author: User
Tags strtok

I recently read "UNIX system programming", and I feel that I can use the C language to get into the classroom.

In general, we will understand the relationship between pointers and arrays. For example, * p is a one-dimensional array, and ** p is a two-dimensional array. In general, it is also rare to see the two-dimensional pointer, but the higher-dimensional ones are afraid that they will be dizzy after a while. There is an example about the parameter list in UNIX system programming. I feel that pointer usage has reached a superb position. So I will post it for your reference.

Multi-level pointer to pointer

In the main function of the C language entry, there is a ** argv parameter that specifies the command line parameter, which is generally written as follows:

C code

 
 
  1. int main(int argc, char **argv){     
  2.     /*    
  3.      * code here.    
  4.      */    
  5. }    
  6.  
  7. int main(int argc, char **argv){  
  8.     /*  
  9.      * code here.  
  10.      */  
  11. }  

This ** argv is a pointer to a pointer used to save the command line parameters. For example, enter a command:

Prog-c-v 200

** The contents in argv are prog,-c,-v, 200. because prog,-c, and so on have different lengths, a pointer is required to reference them, and several parameters behind prog are also not fixed. Therefore, a pointer is required to reference them, so it's the two-dimensional pointer here. It may seem clearer to draw a table:

Prog
-C
-V
200

In another case, the shell program does not know how many lines of commands you will lose, so it needs another pointer to reference how many command inputs you will have. This is what we want to watch today (*** ptr.

Multi-level pointer pointing to "Pointer"

The example in the book is as follows:

C code

 
 
  1. int makeargv(const char *s, const char *delimiters, char ***argvp);    
  2. int makeargv(const char *s, const char *delimiters, char ***argvp);  

The function accepts three parameters. The first is the string to be analyzed, the second is the delimiter sequence, and the third is the pointer to the generated "Pointer" (that is, a two-dimensional array. The implementation is relatively simple, mainly based on the usage of pointers:

C code

 
 
  1. /*
  2. * Author: juntao. qiu
  3. */
  4. Int makeargv (const char * s, const char * delimiters, char *** argvp ){
  5. Int error;
  6. Int I;
  7. Int numtokens;
  8. Const char * snew;
  9. Char * t;
  10. If ((S= NULL) | (Delimiters= NULL) | (Argvp= NULL )){
  11. Error=EINVAL;
  12. Return-1;
  13. }
  14. *Argvp=NULL;
  15. SSnew= S + strspns (s, delimiters );
  16. If ((T=Malloc(Strlen (snew) + 1) = NULL)
  17. Return-1;
  18. Strcpy (t, snew );
  19. Numtokens=0;
  20. If (strtok (t, delimiters )! = NULL)
  21. For (Numtokens=1; Strtok (NULL, delimiters )! = NULL; numtokens ++ );
  22. If ((*Argvp=Malloc(Numtokens + 1) * sizeof (char *) = NULL ){
  23. Error=Errno;
  24. Free (t );
  25. Errno=Error;
  26. Return-1;
  27. }
  28. If (Numtokens= 0 ){
  29. Free (t );
  30. } Else {
  31. Strcpy (t, snew );
  32. **Argvp=Strtok(T, delimiters); // note the pointer operation here.
  33. For (I=1; I< Numtokens; I ++)
  34. * (* Argvp) + I) = strtok (NULL, delimiters); // note the pointer operation here.
  35. }
  36. * (* Argvp) + numtokens) = NULL;
  37. Return numtokens;
  38. }
  39.  
  40. /*
  41. * Author: juntao. qiu
  42. */
  43. Int makeargv (const char * s, const char * delimiters, char *** argvp ){
  44. Int error;
  45. Int I;
  46. Int numtokens;
  47. Const char * snew;
  48. Char * t;
  49.  
  50. If ((S= NULL) | (Delimiters= NULL) | (Argvp= NULL )){
  51. Error=EINVAL;
  52. Return-1;
  53. }
  54.  
  55. *Argvp=NULL;
  56. SSnew= S + strspns (s, delimiters );
  57. If ((T=Malloc(Strlen (snew) + 1) = NULL)
  58. Return-1;
  59.  
  60. Strcpy (t, snew );
  61. Numtokens=0;
  62.  
  63. If (strtok (t, delimiters )! = NULL)
  64. For (Numtokens=1; Strtok (NULL, delimiters )! = NULL; numtokens ++ );
  65.  
  66. If ((*Argvp=Malloc(Numtokens + 1) * sizeof (char *) = NULL ){
  67. Error=Errno;
  68. Free (t );
  69. Errno=Error;
  70. Return-1;
  71. }
  72.  
  73. If (Numtokens= 0 ){
  74. Free (t );
  75. } Else {
  76. Strcpy (t, snew );
  77. **Argvp=Strtok(T, delimiters); // note the pointer operation here.
  78. For (I=1; I< Numtokens; I ++)
  79. * (* Argvp) + I) = strtok (NULL, delimiters); // note the pointer operation here.
  80. }
  81. * (* Argvp) + numtokens) = NULL;
  82.  
  83. Return numtokens;
  84. }

The main body of the program is relatively simple, that is, the program is divided according to the incoming s, according to the delimiter delimiters. After the split, the program is placed in a two-dimensional array, and the first dimension indicates the last array, the second dimension represents the value of each element in the first array.

Test

Okay, let's test its running status:

C code

 
 
  1. int main(int argc, char **argv){     
  2.     char delim[] = " \t";     
  3.     int i;     
  4.     char **argvp;     
  5.     int numtokens;     
  6.     char *test = "mine -c 10 2.0";     
  7.     
  8.     if((numtokens = makeargv(test, delim, &argvp)) == -1){     
  9.         fprintf(stderr, "failed to parse the string you given:%s\n", test);     
  10.         return 1;     
  11.     }     
  12.     printf("argument contains :\n");     
  13.     for(i = 0;i < numtokens;i++)     
  14.         printf("%d:%s\n", i, argvp[i]);     
  15.     return 0;     
  16. }    
  17.  
  18. int main(int argc, char **argv){  
  19.     char delim[] = " \t";  
  20.     int i;  
  21.     char **argvp;  
  22.     int numtokens;  
  23.     char *test = "mine -c 10 2.0";  
  24.  
  25.     if((numtokens = makeargv(test, delim, &argvp)) == -1){  
  26.         fprintf(stderr, "failed to parse the string you given:%s\n", test);  
  27.         return 1;  
  28.     }  
  29.     printf("argument contains :\n");  
  30.     for(i = 0;i < numtokens;i++)  
  31.         printf("%d:%s\n", i, argvp[i]);  
  32.     return 0;  
  33. }   

The running result is as follows:

 
 
  1. C:\development\cpl\usp>ls   
  2. Makefile a.exe makeargv.c nbproject   
  3.  
  4. C:\development\cpl\usp>a   
  5. argument contains :   
  6. 0:mine   
  7. 1:-c   
  8. 2:10   
  9. 3:2.0 

I personally feel that I can use the multi-level pointer to this level of proficiency to be able to master C. The code in "UNIX system programming" is very elegant. I have been a sophomore and I am still reading it after graduation. I will try my best to post my experiences for your reference.

  1. Comprehensive analysis of the concept of C pointer
  2. Tell the story of C and pointer
  3. Effective collection of C ++ reference counting smart pointers
  4. Relationship between arrays and pointer types in C ++
  5. Use C language to edit the drawing program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.