Strcat is a function that links a string to another string. The specific form is as follows:
Char * strcat (char * dest, const char * src)
The specific process of the function is as follows: 1. first look for the end of the dest string (I .e. '\ 0') 2. then, copy the characters (including the ending '\ 0') in src from the end of the dest string '). The result is that the end of dest '\ 0' is overwritten, and a new end is generated. 3. Note 1. dest must be initialized, that is, end with a '\ 0. When you use malloc to allocate strings, there are often no initialization errors. Sometimes it runs normally because the content of the first unit in the allocated memory is exactly '\ 0 '. But it cannot be guaranteed that every time it is so lucky. Therefore, Initialization is necessary. You can initialize char * str = (char *) malloc (sizeof (100); * str = '\ 0'; // note that this is a single quotation mark, think * str indicates a char or * str = 0 in the address where str is stored; or strcpy (str, ""); 2. src must also be initialized, including the end of a '\ 0', but this generally has a low probability of errors, because it is often used as the parameter strcat (dest, "hello world. "). 3. The size of the memory indicated by dest must be greater than or equal to strlen (dest) + strlen (src) + 1. Otherwise, an unknown error may occur, such as garbled characters. Before using the strcat function, make sure that the dest space is large enough. Source code strcat in glibc. cchar * strcat (dest, src) char * dest; const char * src; {char * s1 = dest; const char * s2 = src; reg_char c; /* Find the end of the string. */do c = * s1 ++; while (c! = '\ 0');/* Make S1 point before the next character, so we can increment it while memory is read (wins on pipelined cpus ). */s1-= 2; do {c = * s2 ++; * ++ s1 = c;} while (c! = '\ 0'); return dest;} strcpy is a function that copies a string to another string address. The function format is as follows: char * strcpy (char * dest, char * src) function process 1. copy the characters (including the ending '\ 0') in src to dest one by one, and the original dest characters will be overwritten. 2. precautions for returning the dest pointer: 1. dest can be uninitialized, such as char * str = (char *) malloc (sizeof (char) * 100); strcat (str, "hellow world! "); 2. this feature can be used to initialize the string variable strcat (str, ""); will achieve the same effect as strcpy in glibc. c function implementation char * strcpy (dest, src) char * dest; const char * src; {reg_char c; char * _ unbounded s = (char * _ unbounded) CHECK_BOUNDS_LOW (src); const ptrdiff_t off = CHECK_BOUNDS_LOW (dest)-s-1; size_t n; do {c = * s ++; s [off] = c ;} while (c! = '\ 0'); n = s-src; (void) CHECK_BOUNDS_HIGH (src + n); (void) CHECK_BOUNDS_HIGH (dest + n); return dest ;} is there any tricky implementation in this code? off is (dest address-src address-1) c = * s ++; s [off] = c; it is equivalent to c = * s ++; dest ++ = c; however, I feel that this code format is not recommended, the efficiency is the same, but the readability is worse. It is hard to understand. Let's take a look at the following two sentences. It is more important to improve the efficiency by writing a little bit. What's more, many times the compiler optimization makes those few lines less efficient. For details, refer to the book refactoring to improve the design of existing code. First, I declare that I am not a seller, and I have nothing to do with the author of this book:-D. This article by ladd original, welcome to reprint, but please note the Source: http://www.cnblogs.com/ladd/archive/2012/07/01/2572098.html