C语言 C 中的 strcpy 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14476627/
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 05:07:38  来源:igfitidea点击:

Strcpy implementation in C

cstrcpy

提问by Hommer Smith

So, I have seen this strcpyimplementation in C:

所以,我已经strcpy在 C 中看到了这个实现:

void strcpy1(char dest[], const char source[])
{
    int i = 0;
    while (1)
    {
        dest[i] = source[i];

        if (dest[i] == '
// Move the assignment into the test
void strcpy2(char dest[], const char source[]) 
{
    int i = 0;
    while ((dest[i] = source[i]) != '
char * strcpy(char *strDest, const char *strSrc)
{
    assert(strDest!=NULL && strSrc!=NULL);
    char *temp = strDest;
    while(*strDest++ = *strSrc++); // or while((*strDest++=*strSrc++) != '
char src_str[] = "C programming language";
char dst_str[100];
printf("dst_str: %d\n", strlen(strcpy(dst_str, src_str)));
'); return temp; }
') { i++; } }
') { break; } i++; } }

Which to me, it even copies the \0from source to destination.

对我来说,它甚至可以将\0源复制到目标。

And I have also seen this version:

我也看过这个版本:

char * strcpy(char *strDest, const char *strSrc)
{
    assert(strDest!=NULL && strSrc!=NULL);
    assert(strSrc + strlen(strSrc) < d || strSrc > strDest);  // see note

    char *temp = strDest;
    while(*strDest++ = *strSrc++);
    return temp;
}

Which to me, it will break when trying to assign \0from sourceto dest.

对我来说,尝试\0sourceto分配时它会中断dest

What would be the correct option, copying \0or not?

什么是正确的选择,复制\0与否?

采纳答案by unwind

Both copy the terminator, thus both are correct.

两者都复制终止符,因此两者都是正确的。

Note that strcpy2()does the assignment (the copying) first, thenthe comparison. So it will copy the terminator before realizing it did, and stopping.

请注意,首先strcpy2()进行分配(复制),然后进行比较。所以它会在意识到它之前复制终止符并停止。

Also, note that functions whose names start with strare reserved, so neither of these are actually valid as "user-level" code.

另外,请注意名称以 开头的函数str是保留的,因此这些函数实际上都不是“用户级”代码有效。

回答by herohuyongtao

The code should look like as follows:

代码应如下所示:

//str copy function self made
char *strcpynew(char *d, char *s){
   char *saved = d;
   while ((*d++ = *s++) != '
    void StrCopy( char* _dst, const char* _src )
    {
       while((*_dst++ = *_src++));
    }
'); return saved; //returning starting address of s1 } //default function that is run by C everytime int main(){ //FOR STRCPY char s1[] = "rahul"; //initializing strings char s2[] = "arora"; //initializing strings strcpynew(s1, s2); printf("strcpy: %s\n", s1); //updated string after strcpy }

You can NOTdelete the second line char *temp = strDest;and directly return strDest. This will cause error for the returned content. For example, it will not return correct value (should be 22) will checking the length of returned char *.

不能删除第二行char *temp = strDest;并直接返回strDest。这将导致返回的内容出错。例如,它不会返回正确的值(应该是 22)将检查返回的长度char *

##代码##

回答by m0skit0

You're wrong. Both copy the \0(NUL terminator) character. You have to copy the NUL terminator character always or your string will be broken: you'll never know when/where it ends.

你错了。两者都复制\0(NUL 终止符)字符。您必须始终复制 NUL 终止符,否则您的字符串将被破坏:您永远不知道它何时/何​​地结束。

回答by sr01853

Both strcpy1()and strcpy2()does the same. Both copy the NUL character to the end of the destination array.

无论strcpy1()strcpy2()不一样的。两者都将 NUL 字符复制到目标数组的末尾。

回答by Weimin Ma

##代码##

// without the check on line 4, the new string overwrites the old including the null deliminator, causing the copy unable to stop.

// 没有在第 4 行检查,新字符串覆盖旧字符串,包括空分隔符,导致复制无法停止。

回答by Rahul Arora

Here is full implementation. You do not have to consider the \0 at the end in the first string, it will be copied automatically from the second string as per logic

这是完整的实现。您不必考虑第一个字符串末尾的 \0,它将按照逻辑从第二个字符串自动复制

##代码##

回答by HariRHK

Both copy the terminator, thus both are correct.

两者都复制终止符,因此两者都是正确的。

strcpy2() does the copying first, then the compares. Thus it will copy the terminator and stops.

strcpy2() 首先进行复制,然后进行比较。因此它将复制终止符并停止。

The functions whose names start with str are reserved, so use any other variables or naming types

名称以 str 开头的函数是保留的,因此可以使用任何其他变量或命名类型

回答by Alisher Aliev

You can use this code, the simpler the better ! Inside while() we copy char by char and moving pointer to the next. When the last char \0 will pass and copy while receive 0 and stop.

您可以使用此代码,越简单越好!在 while() 中,我们逐个复制一个字符并将指针移动到下一个。当最后一个字符 \0 将通过并复制时,收到 0 并停止。

##代码##

回答by Davide Berra

Both copy the '\0'. That's what you have to do if you want to fully emulate the original strcpy

两者都复制'\0'。如果您想完全模拟原始 strcpy,这就是您必须做的