- memcpy
#include <stdio.h>
#include <stdlib.h>
#include <memory.h> void * mymemcpy(void* _Dst, void const* _Src, size_t _Size)
{
if (_Dst == NULL || _Src == NULL)
{
return NULL;
} //转化成char类型,一次前进一个字节
char *dst = _Dst;
char *src = _Src; for (int i = ; i < _Size; i++)
{
dst[i] = src[i];
} return _Dst;
} void main()
{
int a[] = { ,,,,,,,,, };
int *p = malloc(sizeof(int) * );
int *tmp = mymemcpy(p, a, ); for (int i = ; i < ; i++)
{
printf("%d\n" ,p[i]);
} char str[] = "hello world";
char mystr[];
mymemcpy(mystr, str, strlen(str) + ); printf("%s\n", mystr); system("pause");
} - memset
void *mymemset(void *_Dst, int _Val, size_t _Size)
{
if (_Dst == NULL || _Val == NULL)
{
return;
} for (int i = ; i < _Size; i++)
{
((char *)_Dst)[i] = _Val;
} return _Dst;
} - memmove
void * mymemmove(void *_Dst, const void *_Src, size_t _Size)
{
if (_Dst == NULL || _Src == NULL)
{
return NULL;
} void *psrc = malloc(_Size);//分配内存
memcpy(psrc, _Src, _Size);
memcpy(_Dst, psrc, _Size);
free(psrc);
return _Dst;
} - memicmp(比较指定字符串前n个字符串)
int mymemicmp(const void *_Buf1, const void *_Buf2, size_t _Size)
{
//保存变量
char *buf1 = _Buf1;
char *buf2 = _Buf2; //结束标识
char *end = buf1 + _Size; while ((*buf1 == *buf2) && buf1 != end)
{
buf1++;
buf2++;
} if (buf1 == end)
{
return ;
}
else
{
return *buf1 - *buf2 > ? : -;
}
} - memchr(寻找一个字符串中是否有指定字符)
void *mymemchr(void *start, char ch, int maxlength)
{
char *p = NULL;
for (int i = ; i < maxlength; i++)
{
if (((char *)start)[i] == ch)
{
p = (char *)start + i;
break;
}
}
return p;
} - memccpy(复制n个字符,遇到指定值退出)
void * mymemccpy(void * _Dst, const void *_Src, int _Val,size_t _MaxCount)
{
char *dst = _Dst;
char *src = _Src; for (int i = ; i < _MaxCount; i++)
{
dst[i] = src[i];
if (dst[i] == _Val)
{
break;
}
}
}