标签:c++ assert ret code def color printf src pause
1、模拟实现memcpy
注意:将*dest =*src放在while判断里直接把*scr里面的 ‘\0‘ 一块拷贝过去了。
1 #include<stdio.h> 2 #include<assert.h> 3 4 5 char* my_memcpy(char* dest, const char* src) 6 { 7 char* ret = dest; 8 assert(dest != NULL); 9 assert(src != NULL); 10 while (*dest++ = *src++) 11 { 12 ; 13 } 14 return ret; 15 } 16 17 int main() 18 { 19 char arr[] = "abcdef"; 20 my_memcpy(arr, "hello"); 21 printf("%s\n", arr); 22 system("pause"); 23 return 0; 24 }
2、模拟实现mommove
1 #include<stdio.h> 2 #include<assert.h> 3 4 char* my_memmove(char* dest, const char* src, size_t num) 5 { 6 char* ret = dest; 7 assert(dest != NULL); 8 assert(src != NULL); 9 while (num--) 10 { 11 *dest++ = *src++; 12 } 13 return ret; 14 } 15 16 int main() 17 { 18 char arr[] = "abcdhello"; 19 my_memmove(arr, arr + 4, 4); 20 printf("%s\n", arr); 21 system("pause"); 22 return 0; 23 }
标签:c++ assert ret code def color printf src pause
原文地址:https://www.cnblogs.com/yishengPan/p/9182144.html