strcat 实现

发布于 2024-08-25 19:14:01 字数 398 浏览 9 评论 0原文

我尝试自己实现 strcat,并从 Wiki 找到了 strcat 实现,如下所示。 ....但是当我使用它时,出现分段错误。

下面的代码有什么问题?

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不羁少年 2024-09-01 19:14:02

代码没问题。

看来您的调用代码有问题。

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

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?

虫児飞 2024-09-01 19:14:02

我强烈建议使用指针而不是整数索引,因为担心整数溢出。即使 size_tchar * 的位数相同,您也会在不添加指针的地方添加索引。

我想这或多或少是学术性的;如果您在数 GB 字符串上调用 strcat(),您可能会遇到各种麻烦。

为了完整起见,这是一个基于指针的版本:

char *
my_strcat(char *dest, const char *src)
{
    char *rdest = dest;

    while (*dest)
      dest++;
    while (*dest++ = *src++)
      ;
    return rdest;
}

当然,这确实需要另一个指针的空间来存储 rdest 返回值,但我认为这是一个很好的权衡。

另请注意,您无法在普通应用程序代码中合法地定义名为 strcat() 的函数;整个命名空间(名称以 str 开头的公共函数)被保留用于实现。

I would strongly suggest using pointers rather than integer indexes, for fear of integer overflow. Even if size_t is the same number of bits as char *, you're adding indices where you wouldn't be adding pointers.

I guess that's more or less academical; if you're calling strcat() on multi-gigabyte strings you're probably in all sorts of trouble.

Here's a pointer-based version, for completeness' sake:

char *
my_strcat(char *dest, const char *src)
{
    char *rdest = dest;

    while (*dest)
      dest++;
    while (*dest++ = *src++)
      ;
    return rdest;
}

Sure, this does take another pointer's worth of space for the rdest return value, but I think that's a fine trade-off.

Also note that you can't legally define a function called strcat() in ordinary application code; that whole namespace (public functions with names starting with str) is reserved for the implementation.

野鹿林 2024-09-01 19:14:02

dest 需要有足够的内存来容纳此实现中的串联。在此实现中,它必须由调用者分配。您还应该确保 dest 和 src 都以 null 结尾。如果 dest 没有足够的内存,这将覆盖可以被其他东西使用的内存。

dest needs to have enough memory to accommodate the concatenation in this implementation. In this implementation, it would have to be allocated by the caller. You should also be sure both dest and src are null terminated. If dest does not have enough memory, this will overwrite memory that could be used by something else.

幸福丶如此 2024-09-01 19:14:02

它对我来说工作正常,我已经检查过。

    #include "stdio.h"


    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;

}


void main(void)

{

    char a[10]={"abc"}, b[10]={"def"};

    strcat(a,b);

    printf("%s",a);

    getchar();

}

Its working fine with me, I have check it.

    #include "stdio.h"


    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;

}


void main(void)

{

    char a[10]={"abc"}, b[10]={"def"};

    strcat(a,b);

    printf("%s",a);

    getchar();

}
浪推晚风 2024-09-01 19:14:02

为目标字符串分配足够的内存..即至少(源字符串的长度+ 1)。

Allocate enough memory for the destination string.. ie atleast (length of source string + 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文