1.memcpy
memcpy是内存拷贝函数,原型为:
void *memcpy(void *dest, const void *src, size_t n);
将n个字节的内存从src拷贝到dest
实现代码:
void *memcpy(void *dest, const void *src, size_t n)
{
assert(dest != NULL && src != NULL);
char *d = (char*)dest;
const char *s = (char*)src;
for(size_t i = 0; i < n; i++)
*d++ = *s++;
return dest;
}
2.memset
memset 是将指针s指向的前n个字节全部置为ASCⅡ值为ch,返回值为指向s的指针,函数原型为
void *memset(void *s, int ch, size_t n);
void *memset(void *s, int ch, size_t n)
{
assert(s != NULL);
const unsigned char uc = ch;
char *temp = (char*)s;
for(size_t i = 0; i < n; i++)
*(temp+i) = uc;
return s;
}