string.h 中 关于数据拷贝的几个函数测试

//strcpy     memcpy   strdup  bcopy

 

#include <stdio.h>
#include <string.h>

#define DEST_ARRY_LEN    16
int my_arry_printf(char * name, char arry [])
{
    int i;
    printf("%s:" ,name);

    for(i  = 0; i < DEST_ARRY_LEN; i ++){
        printf("%c",arry[i]);
    }

    printf(":>/n");

    return 0;
}

int main()
{
    int len = 6;
    char src []= {'1','2','/0','3','4',0};
    char dest [DEST_ARRY_LEN] = {0};

    printf("src  :strlen(%d) sizeof(%d)/n", strlen(src), sizeof(src));
    printf("dest :strlen(%d) sizeof(%d)/n/n", strlen(dest), sizeof(dest));

    memset(dest , 0, sizeof(dest));
    strcpy(dest, src);
    my_arry_printf("strcpy",dest);

    memset(dest , 0, sizeof(dest));
    memcpy(dest, src, len);
    my_arry_printf("memcpy",dest);

    memset(dest , 0, sizeof(dest));
    my_arry_printf("strdup",strdup(src));

    memset(dest , 0, sizeof(dest));
    bcopy(src,dest, len);
    my_arry_printf("bcopy ",dest);

    return 0;
}

/*
结果如下:
src  :strlen(2) sizeof(6)
dest :strlen(0) sizeof(16)

strcpy:12              :>
memcpy:12 34           :>
strdup:12          ? :>
bcopy :12 34           :>
*/

你可能感兴趣的:(c,测试)