Take the String concatenation function as an example strcat.
Prototype: extern char * strcat (char * DEST, char * SRC );
Usage: # include <string. h>
Function: add the SRC string to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0 '.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Example:
// Strcat. c# Include <syslib. h> # include <String. H> main (){CharD [20] ="Golden global";Char* S ="View"; Clrscr (); strcat (D, S); printf ("% S", D); getchar ();Return0 ;}
The above sectionCodeYou can output golden global view without any problems.
But here we will change it like this:
// strcat. c # include
# include
string . h> main () {
char * P =
"golden global" ;
char * s =
"View" ; clrscr (); strcat (p, s); printf (
"% s" , p); getchar ();
return 0 ;}
The parameter meets the requirements of the two pointer parameters, but thisProgramBut cannot run. At first, I was puzzled why the parameter type was correct but the desired result was not returned. In this way, you can only look at the function prototype.
Strcat function prototype
Char * Strcat ( Char * Strdest, Const Char * Strscr) // Add the source string to const, indicating that it is an input parameter { Char * Address = strdest; // If this statement is placed after assert, an error occurs during compilation. Assert (strdest! = NULL) & (strscr! = NULL )); // Add non-0 assertions to the source and target addresses While (* Strdest) // While (* strdest! = '\ 0') Simplified Form { // If you use while (* strdest ++), an error occurs because ++ is not subject to loops. Strdest ++; // Restricted. Therefore, it must be in the body of the loop ++; because if * strdest refers } // End sign '\ 0' to the string '. While (* Strdest ++ = * strscr ++) // While (* strdest ++ = * strscr ++ )! = '\ 0') Simplified Form {NULL; // You can use ++ in this loop condition, } // The statement * strdest = '\ 0' can be added here. Is it necessary? Return Address; // Return the destination address for chained operation }
From this sentence, I will know why.
While (* strdest ++ = * strscr ++)
{
NULL;
}
If strdest is a pointer, * strdest here gets the value of an unknown address, which is intolerable by the compiler. But why is it possible when strdest is an array, because the array is equal to allocating a continuous address to it. Of course, the applied security address can be used. Of course, we can also write a String concatenation function that passes in real pointer parameters. Below is a function prototype I wrote myself:
char * strcatdemo2 ( char * str1, const char * str2) // Add the source string to const, indicating that it is an input parameter {assert (str1! = NULL) & (str2! = NULL); char * address = ( char *) malloc (strlen (str1) + strlen (str2) + 1) * sizeof ( char )); char * des = address; Assert (address! = NULL); while (* str1) {* address = * str1; str1 ++; address ++ ;} while (* str2) {* address = * str2; str2 ++; address ++ ;} * address = '\ 0' ; return des ;}
In this example, the pointer address is requested to store two strings. Note that you need to apply for one more string because the string must end with one '\ 0. You can use it as follows:
IntMain (IntArgc,Char* Argv []) {Char* P ="Hello ,", * S ="World! ";Char* T = strcatdemo2 (P, S); puts (t); System ("Pause");Return0 ;}
The one written above is similar to the function of adding strings in C.
In fact, the strings in most C languages are used by a character array parameter and a character pointer parameter. The following is the prototype of these things, which can take a good look and avoid mistakes in the future.
Strcat function prototype:
Char * Strcat ( Char * Strdest, Const Char * Strscr) // Add the source string to const, indicating that it is an input parameter { Char * Address = strdest; // If this statement is placed after assert, an error occurs during compilation. Assert (strdest! = NULL) & (strscr! = NULL )); // Add non-0 assertions to the source and target addresses While (* Strdest) // While (* strdest! = '\ 0') Simplified Form { // If you use while (* strdest ++), an error occurs because ++ is not subject to loops. Strdest ++; // Restricted. Therefore, it must be in the body of the loop ++; because if * strdest refers } // End sign '\ 0' to the string '. While (* Strdest ++ = * strscr ++) // While (* strdest ++ = * strscr ++ )! = '\ 0') Simplified Form {NULL;// You can use ++ in this loop condition, } // The statement * strdest = '\ 0' can be added here. Is it necessary? Return Address; // Return the destination address for chained operation }
Strcpy function prototype:
char * strcpy ( char * strdest, const char * strscr) { char * address = strdest; Assert (strdest! = NULL) & (strscr! = NULL); while (* strscr) // while (* strscr! = '\ 0'); {* strdest ++ = * strscr ++ ;} * strdest = '\ 0' ; // when the strscr string length is smaller than the original strdest String Length return address; // if the statement is not modified, an error occurs. }
Strcmp function prototype:
int strcmp ( const char * str1, const char * str2) { int Len = 0; Assert (str1! = '\ 0' ) & (str2! = '\ 0' )); while (* str1 & * str2 & (* str1 = * str2) {str1 ++; str2 ++ ;}< SPAN class = "kwrd"> return * str1-* str2 ;}
Strlen function prototype:
IntStrlen (Const Char* Str ){IntLen = 0; Assert (STR! = NULL );While(* STR ++) {Len ++ ;}ReturnLen ;}