C language-pointer exercises, pointer exercises

Source: Internet
Author: User

C language-pointer exercises, pointer exercises

It is difficult to write a good blog

To better understand several difficult concepts in pointers:Pointer array, array pointer, function pointer, pointer function, use function pointer callback function.

1. Use a pointer array to initialize with an indefinite string. The last string ends with @ and outputs all strings and deletes the last @ character.

For example:

Input: aaaaa bbbbb abcdefg asdf @

Output:

Aaaaa

Bbbbb

Abcdefg

Asdf

1 # include <stdlib. h> 2 # include <string. h> 3 int main (int args, const char * argv []) 4 {5 char * p [10] = {NULL}; 6 int cnt = 0; // record the number of input strings 7 // apply for heap memory space 8 for (int I = 0; I <10; I ++) 9 {10 // apply for a memory space of 100 char Types 11 p [I] = (char *) malloc (100 * sizeof (char )); 12 // judge that the application is successful 13 if (! P [I]) 14 {15 return-1; 16} 17 scanf ("% s", p [I]); 18 cnt ++; 19 // determine whether the last character of the string is '@' yes: the replacement bits '\ 0' 20 int len = (int) strlen (p [I]); 21 if (* (p + I) + (len-1) = '@') 22 {23 * (p + I) + (len-1 )) = '\ 0'; 24} 25 // if (p [I] [len-1] = '@') 26 // {27 // p [I] [len-1] = '\ 0'; 28 //} 29 // when a single character bit' \ n' is read, skip loop 30 if (getchar () = '\ n') 31 {32 break; 33} 34} 35 36 for (int I = 0; I <cnt; I ++) 37 {38 printf ("% s \ n", p [I]); 39} 40 return 0; 41}

2.

Function pointer callback

Define three functions at will, use the function pointer to call the three functions, input a n, print n times the Function

1 # include <stdlib. h> 2 # include <string. h> 3 void p_world (void) 4 {5 printf ("world \ n"); 6} 7 void p_hello (void) 8 {9 printf ("hello \ n "); 10} 11 void p_welcome (void) 12 {13 printf ("welcome \ n "); 14} 15 // use the function pointer to call back the above three functions 16 void print (void (* pfunc) (void), int cnt) 17 {18 for (int I = 0; I <cnt; I ++) 19 {20 pfunc (); 21} 22} 23 int main () 24 {25 int n = 0; 26 printf ("Callback times: "); 27 scanf (" % d ", & n); 28 // declare a pointer array to store the function name (Function Location Address) 29 void * pf [3] = {p_world, p_hello, p_welcome}; 30 char * str [3] = {"p_world", "p_hello", "p_welcome "}; 31 char * inputstr = NULL; 32 inputstr = (char *) malloc (100 * sizeof (char); 33 if (! Inputstr) 34 {35 return-1; 36} 37 printf ("callback function name:"); 38 scanf ("% s", inputstr ); 39 for (int I = 0; I <3; I ++) 40 {41 if (! Strcmp (str [I], inputstr) 42 {43 print (pf [I], n); 44} 45} 46 // release memory space 47 free (inputstr ); 48 inputstr = NULL; 49 return 0; 50}View Code

 

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.