C语言 strcat 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2488563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:54:25  来源:igfitidea点击:

strcat implementation

cstrcat

提问by skydoor

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

我自己尝试实现了strcat,从wiki上找到了strcat的实现是这样的……但是我用的时候,出现了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] != '
char *
my_strcat(char *dest, const char *src)
{
    char *rdest = dest;

    while (*dest)
      dest++;
    while (*dest++ = *src++)
      ;
    return rdest;
}
'; i++) ; for (j = 0; src[j] != '
    #include "stdio.h"


    char *strcat(char *dest, const char *src)

    {

    size_t i,j;

    for (i = 0; dest[i] != '##代码##'; i++)

        ;

    for (j = 0; src[j] != '##代码##'; j++)

        dest[i+j] = src[j];

    dest[i+j] = '##代码##';

    return dest;

}


void main(void)

{

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

    strcat(a,b);

    printf("%s",a);

    getchar();

}
'; j++) dest[i+j] = src[j]; dest[i+j] = '##代码##'; return dest; }

回答by Pavel Radzivilovsky

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?

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

回答by unwind

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

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

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.

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

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

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

##代码##

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

当然,这确实为rdest返回值占用了另一个指针的空间,但我认为这是一个很好的权衡。

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.

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

回答by Michael Dean

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.

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

回答by Siddiqui

Its working fine with me, I have check it.

它对我很好,我已经检查过了。

##代码##

回答by Achuthananda MP

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

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