C++ cstring memcpy() function
Example
Copy data from one array to another:
int myNumbers[5] = {10, 20, 30, 40, 50};
int myCopy[5];
memcpy(myCopy, myNumbers, sizeof(myNumbers));
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << " " << myCopy[i] << "\n";
}
Try it Yourself »
Definition and Usage
The memcpy() function copies data from one block of memory to another.
The memcpy() function is defined in the <cstring> header file.
Note: The memcpy() function is generalized for memory of any type. When working with C-style strings (char arrays) it is better to use the strcpy() function instead.
Syntax
memcpy(void * destination, void * source, size_t size);
The size_t data type is a positive integer.
Parameter Values
| Parameter | Description |
|---|---|
| destination | Required. A pointer to the block of memory to be copied to. |
| source | Required. A pointer to the block of memory being copied. |
| size | Required. The amount of data to copy measured in bytes. |
Technical Details
| Returns: | A void type pointer to the destination. |
|---|