C <string.h> - memcpy() Function
The C <string.h> memcpy() function is used to copy num characters of memory block pointed to by source to the memory block pointed to by destination. The behavior of the function is undefined if:
- destination and source overlaps.
- either destination or source is a null pointer.
Syntax
void * memcpy ( void * destination, const void * source, size_t num );
Parameters
destination |
Specify pointer to the first block of memory where the content is to be copied. |
source |
Specify pointer to the second block of memory to copy from. |
num |
Specify number of characters to copy. size_t is an unsigned integer type. |
Return Value
Returns destination
Example:
The example below shows the usage of memcpy() function.
#include <stdio.h> #include <string.h> int main (){ char str[50] = "Hello World!"; char new_str[50]; //copying 5 characters of memory //blocks str into memory block new_str memcpy(new_str, str, 5); //displaying the result printf("new_str contains: %s", new_str); return 0; }
The output of the above code will be:
new_str contains: Hello
❮ C <string.h> Library