C memcpy() Function
You are Here:
C memcpy() Function
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;
}
Output
New dest = 123de
Syntax
void *memcpy(void *dest, const void *src, size_t n)
Parameter Values
Value | Type | Explanation |
---|---|---|
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
Value | Explanation |
---|---|
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;
}
Output
New str1 = ababc
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;
}
Output
memcpy's str1 = ababc
memmove's str2 = ababc
Reminder
Hi Developers, we almost covered 98% of String functions and Interview Question on C with examples for quick and easy learning.
We are working to cover every Single Concept in C.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.