Chapter Contents

Previous

Next
memmove

memmove



Copy Characters

Portability: ISO/ANSI C conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

void *memmove(void *to, const void *from, size_t n);


DESCRIPTION

memmove is identical to memcpy except that the copy is guaranteed to work correctly even if the to and from objects overlap. On completion of the call, the n bytes addressed by to are identical to the n bytes addressed by from before the call.


RETURN VALUE

memmove returns a pointer to the to area.


CAUTION

The third argument to memmove is size_t . If a negative number is passed, massive overlaying of memory may occur.


EXAMPLE

This example removes hyphens from a word by shifting text to the left to overlay any hyphens, using memmove :

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

#define MAXLEN 100

main()
{
   char *word;
   size_t len;
   char *hyphen;
   char line[MAXLEN];

   puts("Enter a hyphenated word: ");
   word = gets(line);
   printf("\noriginal word: %s\n", word);
   len = strlen(word);
   for (;;) {
       hyphen = strchr(word, '-');
       if (!hyphen) break;           
       memmove(hyphen, hyphen + 1, len - (hyphen - word));
          /* Remove hyphen from word. */
       --len;
   }
   printf("Unhyphenated word: %s\n", word);
}


RELATED FUNCTIONS

memcpy


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.