标签:div ret col mem 测试用例 psr rgb test mys
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++) != ‘\0‘); 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\n", buf+2); } void testStrcpy(){ char src[40]; char dest[100]; memset(dest, ‘\0‘, sizeof(dest)); char *dest1 = myStrcpy(src, "this is test of strcpy"); printf("dest1 = %s\n" , dest1); myStrcpy(dest, src); printf("dest = %s\n" , dest); }
标签:div ret col mem 测试用例 psr rgb test mys
原文地址:https://www.cnblogs.com/wanghao-boke/p/14660408.html