模拟实现C语言库函数strcpy

首先,我们来看一下MSDN中对strcpy的定义,函数参数,返回值
strcpy
(定义)Copy a string.

(函数的返回类型及参数)char *strcpy( char *strDestination, const char *strSource );

(返回值)Each of these functions returns the destination string. No return value is reserved to indicate an error

接下来看一些代码实现

#include<stdio.h>
#include<assert.h>
#include<windows.h>
char * my_strcpy(char* dest, const char* cpy)
{char
    assert(dest != NULL);
    assert(cpy != NULL);
    char* str = dest;
    while (*dest++ = *cpy++)//先赋值,再判断,此时把'\0'已经拷入,然后判断为0,跳出循环
    {

        ;
    }
    return str;
}
int main()
{
    char* arr = "abcdefqwer";
    char arr1[] = {0};
    char* str1 = my_strcpy(arr1, arr);
    printf("%s",str1);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aixintianshideshouhu/article/details/81216155