.TH memmove() "" "" "String Function (libc)"
.PC "Copy region of memory into area it overlaps"
.B "#include <string.h>"
\fBchar *memmove(\fIregion1\^\fB, \fIregion2\^\fB, \fIcount\^\fB)\fR
\fBchar *\fIregion1\^\fB, char *\fIregion2\^\fB, unsigned int \fIcount\^\fB;\fR
.PP
.II "character, copy"
.II "region of memory, copy"
.II "memory, copy"
.II "copy a region of memory"
.B memmove()
copies
.I count
characters from
.I region2
into
.IR region1 .
Unlike
.BR memcpy() ,
.B memmove()
correctly copies the region pointed to by
.I region2
into that pointed by
.I region1
even if they overlap.
To \*(QLcorrectly copy\*(QR means that the overlap does not
propagate, not that the moved data stay intact.
Unlike the string-copying routines
.B strcpy()
and
.BR strncpy() ,
.B memmove()
continues to copy even if it encounters a NUL.
.PP
.B memmove()
returns
.IR region1 .
.SH Example
The following example rotates a block of memory by one byte.
.DM
#include <string.h>
#include <stddef.h>
#include <stdio.h>
.DE
.DM
char *
rotate_left(region, len)
char *region; size_t len;
{
	char sav;
.DE
.DM
	sav = *region;
	/* with memcpy this might propagate the last char */
	memmove(region, region + 1, --len);
	region[len] = sav;
	return(region);
}
.DE
.DM
char nums[] = "0123456789";
main(void)
{
	printf(rotate_left(nums, strlen(nums)));
	return(EXIT_SUCCESS);
}
.DE
.SH "See Also"
.Xr "libc," libc
.Xr "string.h" string.h
.br
\*(AS, \(sc7.11.2.2
.SH Notes
.I region1
should point to enough reserved memory to hold the contents of
.IR region2 .
Otherwise, code or data will be overwritten.
