zoukankan      html  css  js  c++  java
  • strcpy和memcpy用法(待完善测试用例)

    strcpy 和 memcpy 的区别

    源码实例:

    #include<cstdio>
    #include<cstring>
    #include<cassert>
    
    char *myStrcpy(char* dest, const char* src){
        if ((NULL == dest) || (NULL == src)){
            return NULL;
        }
    
        char *strDest = dest;
        const char *strSrc = src;
        while ((*dest++ = *strSrc++) != '');
        return strDest;    
    }
    
    void *myMemcpy(void *dest, const void *src, size_t size){
        if(NULL == dest || NULL == src){
            return NULL;
        }
    
        char *pDest = NULL;
        char *pSrc = NULL;
    
        if ((src < dest) && ((char*)src + size > (char*)dest)){
            pDest = (char *)dest + size - 1;
            pSrc = (char *)src + size - 1;
            while(size--){
                *pDest-- = *pSrc--;
            }
        }
        else{
            pSrc = (char *)src;
            pDest = (char *)dest;
            while (size--)
            {
                *pDest++ = *pSrc;
            }
        }
        
        return dest;
    }
    
    
    void testStrcpy();
    void testMemcpy();
    
    int main(){
        // testStrcpy();
        testMemcpy();
    
    }
    
    void testMemcpy(){
        char buf[100] = "abcdefghijklmn";
        myMemcpy(buf+2, buf, 4);
        printf("buf = %s
    ", buf+2);
    
    }
    
    void testStrcpy(){
        char src[40];
        char dest[100];
    
        memset(dest, '', sizeof(dest));
        char *dest1 = myStrcpy(src, "this is test of  strcpy");
        printf("dest1 = %s
    " , dest1);
        myStrcpy(dest, src);
        printf("dest = %s
    " , dest);
    }
  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    PHP中foreach用法详细讲解
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/14660408.html
Copyright © 2011-2022 走看看