C标准库--memcpy、memmove函数
1、The memcpy function
1.1、Synopsis
#include <string.h>
void *memcpy(void *s1, const void *s2, size_t n);
1.2、Description
The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undeined.
1.3、Returns
The memcpy function returns the value of s1.
1.4、Implementing
/* memcpy function */
#include <string.h>
void *(memcpy)(void *s1, const void *s2, size_t n)
{
char *su1;
const char *su2;
for (sul = s1, su2 = s2; 0 < n; ++su1, ++su2; --n)
*su1 = *su2;
return (s1);
}
1.5、Using
2、The memcpy function
2.1、Synopsis
#include <string.h>
void *memmove(void *s1, const void *s2, size_t n);
2.2、Description
The memmove function copies n characters from the object pointed toby s2 into the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n character thar does not overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1.
2.3、Returns
The memmove function returns the value of s1.
2.4、Implementing
/* memmove function */
#include <string.h>
void * (memmove)(void *s1, const void *s2, size_t n) /* copy char s2[n] to s1[n] safely */
{
char *sc1;
const char *sc2;
sc1 = s1;
sc2 = s2;
if (sc2 < sc1 && sc1 < sc2 + n)
for (sc1 += n, sc2 += n; 0 < n; --n) /* copy backwards */
*--sc1 = *--sc2;
else
for (; 0 < n; --n) /* copy forwards */
*sc1++ = *sc2++;
return s1;
}