memmove与memcpy的区别在于memmove可是实现数据重叠的拷贝,而memcpy只能拷贝不重叠的数据。
memmove的实现
void* memmove(void* dst, const void* src, size_t count)
{
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count))
{
while (count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else
{
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return ret;
}
memcpy的实现
void* memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
assert(dst);
assert(src);
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return ret;
}
strcmp的实现
int strcmp(const char* str1, const char* str2)
{
assert(str1 != NULL && str2 != NULL);
while (*str1 && *str1 == *str2)
{
str1++;
str2++;
}
if (*(unsigned char*)str1 < *(unsigned char*)str2)
return -1;
else if (*(unsigned char*)str1 > *(unsigned char*)str2)
return 1;
else
return 0;
}
strcat的实现
char* my_strcat(char* arr1, const char* arr2)
{
assert(arr1 != NULL && arr2 != NULL);
char* address = arr1;
while (*arr1 != '\0')
arr1++;
while (*arr2 != '\0')
*arr1++ = *arr2++;
*arr1 = '\0';
return address;
}
strlen的实现
int my_strlen(char* arr)
{
int count = 0;
while (*arr != '\0')
{
arr++;
count++;
}
return count;
}
strcpy的实现
strcpy与memcpy的区别在于memcpy是拷贝内存空间,而strcpy是字符串的拷贝
char* my_strcpy(char* arr1, const char* arr2)
{
assert(arr1 != NULL && arr2 != NULL);
char* tmp = arr1;
while (*arr1++ = *arr2++)
{
;
}
return tmp;
}