C memcpy() Function

Last Updated:

C memcpy()

The memcpy() function copies a block of memory from a location to another.

This function copies the data directly from source to destination. So, memcpy() is faster approach than memmove().

This function overlaps the source. So, memmove() is not as safe as memmove().

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char dest[] = "abcde"; char src[] = "123"; memcpy(dest, src, 3); printf("New dest = %s", dest); return 0; }

Syntax

void *memcpy(void *dest, const void *src, size_t n)

Parameter Values

ValueTypeExplanation
dest Required Destination array where the content is to be copied.
src Required Source of data to be copied.
n Required Number of bytes to be copied.

Return Value

ValueExplanation
Address Returns a pointer to the destination, which is dest array.

More Examples

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcde"; memcpy(&str1[2], &str1[0], 3); printf("New str1 = %s", str1); return 0; }

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcde"; char str2[] = "abcde"; memcpy(&str1[2], &str1[0], 3); memmove(&str2[2], &str2[0], 3); printf("memcpy's str1 = %s\n", str1); printf("memmove's str2 = %s", str2); return 0; }

Share this Page

Meet the Author