memcpy
Function Usage
Copies content in the source memory block to the destination memory block. The two memory blocks cannot overlap. Otherwise, undefined behavior may occur.
Function Syntax
void *memcpy(void *dest, const void *src, size_t num);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
dest |
Pointer to the destination memory used to store the copied data. |
Non-null pointer to a valid memory block, with at least num bytes accessible. |
Input/Output |
src |
Pointer to the source memory from which data is to be copied. |
Non-null pointer to a valid memory block, with at least num bytes accessible. |
Input |
num |
Number of copied bytes. |
Non-negative number that does not exceed the memory size. |
Input |
Return Value
- Success: A pointer to the destination memory is returned.
- Failure: See those of open source glibc. No other exception values will be returned.
If the source and destination memory blocks overlap, use memmove instead because memcpy cannot securely handle the overlapped memory.
Example
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
memcpy(destination, source, strlen(source) + 1);
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
Output:
Source: Hello, World! Destination: Hello, World!
Parent topic: Function Syntax