6.20.5 memcpy Function

Copies characters from one buffer to another.

Include

<string.h>

Prototype

void *memcpy(void *dst, const void *src, size_t n);

Arguments

dst
buffer to copy characters to
src
buffer to copy characters from
n
number of characters to copy

Return Value

Returns dst.

Remarks

memcpy copies n characters from the source buffer src to the destination buffer dst. If the buffers overlap, the behavior is undefined.

For MPLAB XC16 or XC-DSC functions that can copy to/from specialized memory areas, such as memcpy_eds or memcpy_p2d1624, see “Functions for Specialized Copying and Initialization” in the library reference manual relevant for your compiler..

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  char buf1[50] = "";
  char buf2[50] = "Where is the time?";
  char buf3[50] = "Why?";

  printf("buf1 : %s\n", buf1);
  printf("buf2 : %s\n", buf2);
  printf("buf3 : %s\n\n", buf3);

  memcpy(buf1, buf2, 6);
  printf("buf1 after memcpy of 6 chars of "
         "buf2: \n\t%s\n", buf1);

  printf("\n");

  memcpy(buf1, buf3, 5);
  printf("buf1 after memcpy of 5 chars of "
         "buf3: \n\t%s\n", buf1);
}

Example Output

buf1 :
buf2 : Where is the time?
buf3 : Why?

buf1 after memcpy of 6 chars of buf2:
        Where

buf1 after memcpy of 5 chars of buf3:
        Why?