strcat的实施 [英] strcat implementation

查看:104
本文介绍了strcat的实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试自己实现的strcat,我发现从维基中的strcat的实施像这个......但是当我使用它,有段错误。

这有什么错低于code?

 的char *
strcat的(字符* DEST,为const char * SRC)
{
    为size_t I,J;
    对于(i = 0; DEST [I] ='\\ 0';!我++)
        ;
    为(J = 0; SRC [J] ='\\ 0';!J ++)
        DEST [I + J] = SRC [J]。
    DEST [I + J] ='\\ 0';
    返回DEST;
}


解决方案

在code是好的。

看起来你必须在调用code的一个问题。

你记得为目标字符串分配足够的内存?

I tried to implement the strcat by myself, and I found the strcat implementation from Wiki like this......but when I use it, there is segmentation fault.

What's wrong with the code below?

char *
strcat(char *dest, const char *src)
{
    size_t i,j;
    for (i = 0; dest[i] != '\0'; i++)
        ;
    for (j = 0; src[j] != '\0'; j++)
        dest[i+j] = src[j];
    dest[i+j] = '\0';
    return dest;
}

解决方案

the code is okay.

Looks like you have a problem in the calling code.

Did you remember to allocate enough memory for the target string?

这篇关于strcat的实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆