First, using the pointer out of the decomposed number
The effect of implementing a method "multiple return Values": Returns a three-digit digit, 10 bits, and hundreds. Here, we use pointers to "return" the results of the decomposition. This application is also the most common pointer application.
//fun1: Breaks down a three-digit number, passing single-digit, 10-bit, and hundred digitsintParsenumber (intNumint* g,int* S,int*b) { if(Num < -|| num >999) { //only 100~999 numbers are allowed return-1; } *g = num%Ten; *s = (num/Ten)%Ten; *b = (num/ -)%Ten; return 1;}
The test number is 365, and the Parsenumber function is called, and the address of the value to be returned is passed as a parameter:
int Main (intChar *argv[]) { int365; int g,s,b; if (Parsenumber (num,&g,&s,&b)) { printf ("%d%d%d\n", B,s,g); } return 0 ; }
The results of the operation are as follows:
Second, self-implementation strlen () function
We know that strlen is the length of the computed string, and its interior is judged by whether it is a terminator, although it is not a safe way, but it is the most common function in the major C textbooks. Here, we are going to implement a strlen function that uses pointer movement to assist in calculating the length, which is also one of the most common applications of pointers.
// fun2: Self-simulation to implement Strlen function int mystrlen (char* str) { int0; while ' / ' ) { str+ +; Len++ ; } return Len;}
The test string is "Hello" and the length of the 5,main function is declared as follows:
int Main (intChar *argv[]) { char"Hello" ; int len = mystrlen (test); printf ("Thelength of ' hello ' is%d\n", Len); return 0 ;}
The results of the operation are as follows:
Third, determine whether string a ends with string b
Determines whether a string ends with another substring, a method that belongs to the string class in Java, called EndsWith. In C #, it is also located in the String class, called EndsWith, and provides three overloads. Here, we use pointers to assist in the implementation of this classic EndsWith method.
//Fun3: Determines whether string a ends with string bintEndsWith (Char* STR,Char*substr) { intstrlen =0; while(*str! =' /') {str++; Strlen++; } intSublen =0; while(*substr! =' /') {substr++; Sublen++; } if(Strlen <Sublen) { //if the substring length is longer than the main string return-1; } inti; for(i=0; i<=sublen;i++) { //compare each character starting at the end CharStrch = *str; CharSUBCH = *substr; if(Strch! =subch) { return 0; } Else{str--; SUBSTR--; } } return 1;}
The method mainly goes through two steps, one is to calculate the length of the main string and the substring, if the substring length exceeds the length of the main string, then return 0 directly, which means that the main string does not end with a substring, and the second is to compare each character of the main string and substring from the end until the end of the loop If there is a difference, return 0 directly.
The following is a simple test of this endswith function, which gives a suitable use case and an inappropriate use case, respectively:
intMainintargcChar*argv[]) { CharStr[] ="Edisonchou"; CharSub[] ="Chou"; printf ("%d\n", EndsWith (str,sub)); CharSub2[] ="Zhou"; printf ("%d\n", EndsWith (STR,SUB2)); return 0;}
The results of the operation are as follows:
Resources
such as Peng Network, "C language can also do big Things (third edition)"
Zhou Xurong
Source: http://edisonchou.cnblogs.com
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
You must know the pointer base--5. Several small application cases of pointers