MDGSF Software Engineer

[C/C++] strcpy 库函数实现

2016-12-09
 

#include <stdio.h>
#include <assert.h>

//pcSrc 加上 const
//返回 pcDest,支持链式操作
char * MyStrcpy(char * pcDest, const char * pcSrc)
{
    //判断是否为NULL
    assert( (pcDest != NULL) && (pcSrc != NULL) );

    //判断是否重叠
    if(pcDest == pcSrc)
    {
        return pcDest;
    }

    char * pcAddr = pcDest;
    while ( *pcDest++ = *pcSrc++ )
    {
        ;
    }

    return pcAddr;
}

int main()
{
    printf("MyStrcpy\n");
    char ac[1024] = {0};
    MyStrcpy(ac, "Hello world");
    printf("ac = [%s]\n", ac);
    return 0;
}

weixingongzhonghao

Similar Posts

Comments