The C library memcpy() function is also known as Copy Memory Block function / Memomy to Memory Copy. It is used to specify the range of characters which could not exceed the size of the source memory.
SyntaxFollowing is the syntax of the C library memcpy() function −
void *memcpy(void *dest_str, const void * src_str, size_t n)Parameters
This function accepts the following parameters−
dest_str − This parameter define a pointer to the destination array where the content is to be copied. It is type-casted to a pointer of type void*.
src_str − This parameter is used to define the source of data to be copied. It is then type-casted to a pointer of type void*.
n − This parameter refer to number of bytes to be copied.
This function returns a pointer to destination i.e. dest_str.
Example 1The C library function memcpy() uses three parameters− destination string(dest), source string(src), and strlen() function where it calculates the length of the source string and the number of bytes to be copied.
#include <stdio.h> #include <string.h> int main () { const char src[50] = "Tutorialspoint"; char dest[50]; strcpy(dest,"Heloooo!!"); printf("Before memcpy dest = %s\n", dest); memcpy(dest, src, strlen(src) + 1); printf("After memcpy dest = %s\n", dest); return(0); }Output
The above code produces following result−
Before memcpy dest = Heloooo!! After memcpy dest = TutorialspointExample 2
Below the program uses two functions− puts() and memcpy() to copy the content from one memory address/location to another.
#include <stdio.h> #include <string.h> int main() { char first_str[] = "Tutorials"; char sec_str[] = "Point"; puts("first_str before memcpy:"); puts(first_str); // Copy the content of first_str to sec_str memcpy(first_str, sec_str, sizeof(sec_str)); puts("\nfirst_str after memcpy:"); puts(first_str); return 0; }Output
On execution of above code, we get the following result−
first_str before memcpy: Tutorials first_str after memcpy: PointExample
Following is the C program that demonstrate the code snippet of memcpy() function to represent the text before and after using certain action or process.
#include <stdio.h> #include <string.h> #define MAX_CHAR 100 int main() { char first_str[MAX_CHAR] = "Hello World!"; char second_str[MAX_CHAR] = "Welcome to Tutorialspoint"; printf("The Actual Statements:-\n"); printf("first_str: %s\n", first_str); printf("second_str: %s\n", second_str); //copying all bytes of second_str to first_str memcpy(first_str, second_str, strlen(second_str)); printf("After executing the function memcpy()\n"); printf("first_str: %s\n", first_str); printf("second_str: %s\n", second_str); return 0; }Output
After executing the code, we get the following result−
The Actual Statements:- first_str: Hello World! second_str: Welcome to Tutorialspoint After executing the function memcpy() first_str: Welcome to Tutorialspoint second_str: Welcome to Tutorialspoint
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4