memcpy () function of C function use __ function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.