This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
memmove
Prototype
#include <string.h>
void *memmove(void *dest, const void *src, size_t n);
Description
Copies the data pointed by src
into the dest
buffer, overwriting
the previous existing data. The size in bytes to be copied is specified
by the n
parameter. The two areas can overlap.
Returns the dest
parameter.
Implementation
#include <string.h>
void *memmove(void *dest, const void *src, size_t n)
{
char *dp = dest;
const char *sp = src;
if (dest < src)
while (n-- > 0)
*dp++ = *sp++;
else {
dp += n;
sp += n;
while (n-- > 0)
*--dp = *--sp;
}
return dest;
}
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char buf[8];
strcpy(buf, "abcdef");
strcpy(buf + 1, buf); /* Undefined behavior. */
strcpy(buf, "abcdef");
memmove(buf + 1, buf, 7); /* OK. */
printf("%s\n", buf); /* Prints "aabcdef". */
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.11.2.2