函数原型:
char* strcpy(char *dst, const char *src);
char* memcpy(void *dst, const void *src, const size_t len);
char* memmove(void *dst, const void *src, const size_t len);
char* memset(void *dst, const int c, const size_t len);
实现:
char* mystrcpy(char *dst, const char *src){
assert(dst != NULL && src != NULL);
char *d = dst;
while ((*dst = *src) != '\0');
return d;
}
char* mymemcpy(void *dst, const void *src, const size_t len){
assert(dst != NULL && src != NULL);
char *d = (char*)dst;
char *s = (char*)src;
for (int i = 0; i < len; ++ i){
d[i] = s[i];
}
return dst;
}
void* mymemmove(void *dst, const void *src, const size_t len){
assert(dst != NULL && src != NULL);
char *d = (char*)dst;
char *s = (char*)src;
char tmp[len];
for (int i = 0; i < len; ++ i) {
tmp[i] = s[i];
}
for (int i = 0; i < len; ++ i) {
d[i] = tmp[i];
}
return dst;
}
void* mymemset(void *dst, const int c, const size_t len){
assert(dst != NULL);
char *d = (char *)dst;
for (int i = 0; i < len; ++ i) {
d[i] = c;
}
return dst;
}