Chapter Contents

Previous

Next
memcpy

memcpy



Copy Characters

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

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


DESCRIPTION

memcpy copies the number of bytes specified by n from one area of memory ( from ) to another ( to ). All bytes, including any null characters, are copied.


RETURN VALUE

memcpy returns a pointer to the to area.


CAUTION

The effect of memcpy when the source and target fields overlap is undefined. Sometimes a run-time diagnostic message is produced in this case.

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


IMPLEMENTATION

The compiler generates inline code for memcpy unless memcpy is undefined (by an #undef statement) to prevent this. The inline code may still call a library routine in special cases (for example, if the length is a variable whose value is larger than 16 megabytes).

The code generated for memcpy usually uses the MVCL instruction to perform data movement. If more than 16 megabytes of data are to be moved, the library routine is called, which moves 16M-1 bytes at a time. (Thus, the effect on overlapping fields can depend on whether they are separated by as much as 16 megabytes.) For more information on optimizing your use of memcpy , see Optimizing Your Use of memcmp, memcpy, and memset.


EXAMPLE

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

#define TAILSIZE 10

main()
{
   char buf[160];
   char tail[TAILSIZE+1];

   puts("Enter a line of text.");
   gets(buf);
   if (strlen(buf) < TAILSIZE)
      printf("Your input was shorter than %d characters.\n",
             TAILSIZE);
   else{
      memcpy(tail, buf+strlen(buf)-TAILSIZE, TAILSIZE+1);

         /* Copy last 10 characters of buf, */
         /* plus the trailing null.         */
      printf("The last 10 characters of your input were "
                "\"%s\".\n", tail);
   }
}


RELATED FUNCTIONS

memcpyp , memmove , strcpy , strncpy


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

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