c memcpy array
memcpy
is a function in the C programming language that is used to copy a block of memory from one location to another. It takes three arguments: the destination pointer, the source pointer, and the number of bytes to be copied. The function is defined in the string.h
header file.
Here is the syntax of the memcpy
function:
void memcpy(void destination, const void *source, size_t num);
The destination
parameter is a pointer to the memory location where the copied data will be stored. The source
parameter is a pointer to the memory location from where the data will be copied. The num
parameter specifies the number of bytes to be copied.
The memcpy
function returns a pointer to the destination memory location.
Here is an example usage of the memcpy
function:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
memcpy(destination, source, strlen(source) + 1);
printf("Copied string: %s\n", destination);
return 0;
}
In this example, the memcpy
function is used to copy the contents of the source
array to the destination
array. The strlen(source) + 1
is used as the length parameter to ensure that the null-terminating character is also copied.
The output of this program will be:
Copied string: Hello, World!
This demonstrates how the memcpy
function can be used to copy the contents of one array to another in C.