strcat函数实现

 函数功能:把 source 所指向的字符串追加到 destination所指向的字符串的结尾。

char * strcat ( char * destination, const char * source )
{
    assert((destination!=NULL) && (source!=NULL));
    char* ret = destination;
    
    while(*(++destination));//指针移动到末尾

    while(*destination++ = *source++);

    return ret;
}

注意:

做这类题目需要时刻清楚++i和i++的区别;

* 和 ++是同一级优先级,都是单目运算符,右结合

不能写成 while(*destination++);这样会先赋值判断是否为 '\0' ,然后指针加1,后面的while再使用指针destination时,指针已经移到 '\0' 后面了。

发布了21 篇原创文章 · 获赞 5 · 访问量 2257

猜你喜欢

转载自blog.csdn.net/PTA123/article/details/105174167