Function prototypes
void *memcpy (void*dest, const void *SRC, size_t n);
Function
The continuous n-byte data directed by SRC to the address as the starting address is copied to the space with Destin pointing to the address as the starting address.
Header file
#include <string.h>
return value
function returns a pointer to a dest.
Description
1.source and Destin cannot overlap the memory area, and the function returns a pointer to the Destin.
2. Compared with strcpy, memcpy is not the end of "the", but will definitely copy n bytes.
memcpy is used to make a memory copy, you can copy any data type object, you can specify the data length of the copy;
Cases:
Char a[100], b[50];
memcpy (b, a,sizeof (b)); Note that if you use sizeof (a), you will overflow the memory address of B.
strcpy can only copy the string, it encounters the "" "to end the copy;
Char a[100], b[50];
strcpy (A,B);
3. If the target array Destin itself has data, the original data (up to N) will be overwritten after the memcpy () is executed. If you want to append data, add the destination array address to the address you want to append to the data each time you execute memcpy.
Note that both source and Destin are not necessarily arrays, and any writable space is available.
One of the applications:
Efficiently copy memory data and can be automatically converted by data type:
Convert an integer type data to a string (4 characters in length, 32 bits, because the length of the integer type is 32 bits, 4 bytes), and then efficiently convert the string to an integer.
#include <string>
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int c = m;
Char b[4];
memcpy (b, &c, sizeof (int));
cout<<c<<endl;
cout<<b<<endl;
int D;
memcpy (&d, B, sizeof (int));
cout<<d<<endl;
return 0;
}
The output results are:
100 D
See leetcode449 for detailed application. Serialize and Deserialize BST