mmeemmmmoovvee() -- String Function (libc)

Copy region of memory into area it overlaps
#iinncclluuddee <ssttrriinngg.hh>
cchhaarr *mmeemmmmoovvee(_r_e_g_i_o_n_1, _r_e_g_i_o_n_2, _c_o_u_n_t)
cchhaarr *_r_e_g_i_o_n_1, cchhaarr *_r_e_g_i_o_n_2, uunnssiiggnneedd iinntt _c_o_u_n_t;

mmeemmmmoovvee()  copies  _c_o_u_n_t  characters  from  _r_e_g_i_o_n_2  into  _r_e_g_i_o_n_1.  Unlike
mmeemmccppyy(), mmeemmmmoovvee() correctly copies  the region pointed to by _r_e_g_i_o_n_2 into
that pointed by _r_e_g_i_o_n_1 even  if they overlap.  To ``correctly copy'' means
that the overlap  does not propagate, not that the  moved data stay intact.
Unlike  the  string-copying  routines  ssttrrccppyy()  and  ssttrrnnccppyy(),  mmeemmmmoovvee()
continues to copy even if it encounters a NUL.

mmeemmmmoovvee() returns _r_e_g_i_o_n_1.

_E_x_a_m_p_l_e
The following example rotates a block of memory by one byte.

#include <string.h>
#include <stddef.h>
#include <stdio.h>

char *
rotate_left(region, len)
char *region; size_t len;
{
    char sav;

    sav = *region;
    /* with memcpy this might propagate the last char */
    memmove(region, region + 1, --len);
    region[len] = sav;
    return(region);
}

char nums[] = "0123456789";
main(void)
{
    printf(rotate_left(nums, strlen(nums)));
    return(EXIT_SUCCESS);
}

_S_e_e _A_l_s_o
lliibbcc, ssttrriinngg.hh
ANSI Standard, section 7.11.2.2

_N_o_t_e_s
_r_e_g_i_o_n_1  should point  to enough  reserved memory to  hold the  contents of
_r_e_g_i_o_n_2. Otherwise, code or data will be overwritten.
