C Language Implementation strcat

Source: Internet
Author: User

First look at the code:

1 #ifndef Strcat_h2 #defineStrcat_h3 4 /*******************************************************************5 prototype: extern char *strcat (char *dest,char *src);6 7 strcat () copies the parameter SRC string to the trailing end of the string referred to by the parameter dest;8 dest The final end character, NULL is overwritten, and a null is added to the tail of the concatenated string. 9 Ten returns a pointer to the dest.  One  A Note: The memory space referred to by dest and SRC cannot overlap, - and dest to have enough space to accommodate the string to be copied.  -  the ******************************************************************* - Expansion: - * There are two meanings, theoretically - as a multiplication operator, the level is lower than + + (self-increment).  + as the pointer-value operator, the level is the same as + + (self-increment).  -  + *p++; very confusing.  It is not *p = *p + 1; But *p = * (p+1); A *******************************************************************/ at  -#include <stdio.h> -  - Char*cat_stacat (Char*DST,Const Char*src) { -     if(Null = = DST && NULL = =src) -         returnNULL; in      -     Char*ADDR =DST; to  +     //Method 1: -     //Notice: Note the difference between methods 1 and 2! In Method 1, the last cycle of DST is added 1, which eventually points to the next byte of the string Terminator ' \ '
If not--dst. The end may be "Hello\0world", which will only output hello
the /*while (*dst++); * --DST;*/ $ Panax Notoginseng //Method 2: - while(*DST) the++DST; + A while(*dst++ = *src++) ; the +*DST =' /';// - $ $ returnAddr//returns a pointer to DST - } - the - #endif

Main

1#include"strcat.h"2 3 voidTest_strcat ();4 5 intMain () {6 7 Test_strcat ();8 9     return 0;Ten } One  A voidTest_strcat () { -     Chardst[ -] = {"Hello"};//ensure that DST has enough space -      the     Char*ret = Cat_stacat (DST,"world!"); -  -printf"%s\n%s\n", ret, DST); -}

Attention!!!

1 // Method 1: 2     // Notice: Note the difference between methods 1 and 2! In Method 1, the last cyclic DST is added 1, which eventually points to the next byte of the string Terminator ' \ ' 3      while(*dst++4     --  56     //  Method 2:7while      (*   8         ++dst;

Notice the difference between the 2 ways!!!

Let me see while (*p++!) = ' + ') secret. The original thought that after jumping out, p points to ', ' but in fact it is not! This way, how to splice can not be realized. To verify the problem, let's start with a small program:

1#include <stdio.h>2 3 voidMain ()4 5 {6 7    Char*p ="ABCDEFG";8 9     while(*p++! ='C');Ten  Oneprintf"%c\n", *p); A  -}

What do you guess is printed out? The result is D.

Why is it?

Originally * and + + priority is the same, when the same priority, the program is executed from left to right. So when *p = ' C ' or *p = ' + ', p still moves down one bit, that is, p points to the next byte after the condition is met. (The last loop in the Cat_strcat function code has a 1 DST, which eventually points to the next byte of the string Terminator ' \ ")

Because of the ' *p++ ' Terminator, if, while when it will be sentenced to 0, so the program is written as while, the effect is the same, the final program points to the end of the string "\" the next byte.

If you write this:

while (*p) p+ +;

When the *p= ' + ' is not in the loop inside the while, so this writing jumps out of the loop, p points to '. Comrades noticed that there was no ";" in the back of the while.

//===========================================================//

Finally, the main function outputs the RET and the DST string is the same!!

Why do you need to return that value, since it is the same? Wouldn't it be superfluous?

The reason is very simple, when we write C code, we often use a chain call.

That is, by returning to the first address can be achieved like strcat (strcat (DST, str1), str2); the same usage!!!

In front of the implementation of the STRCPY blog post also said this: http://www.cnblogs.com/lingshaohu/p/3961132.html

Reference: http://blog.csdn.net/yanzi1225627/article/details/7843672

C Language Implementation strcat

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.