memcpy usage of C language

Source: Internet
Author: User

Reprint please specify the original link, http://www.cnblogs.com/flyingcloude/p/6992215.html

These days often encounter memcpy these functions, for the memcpy function before the general role of some understanding, but in peacetime to write their own programs, basically do not use this function. Go to Google today, find out the following paragraph of this note:

Description
Prototype: extern void *memcpy (void *dest, void *src, unsigned int count);

Usage: #include <string.h>

Function: the memory area referred to by SRC is copied from count bytes to the memory area of Dest.

Description: The memory area referred to by SRC and dest cannot overlap, and the function returns a pointer to Dest.

Example:

Memcpy.c

#include <syslib.h>
#include <string.h>

Main ()
{
Char *s= "Golden Global View";
Char d[20];

CLRSCR ();

memcpy (D,s,strlen (s));
D[strlen (s)]=0;
printf ("%s", d);

GetChar ();
return 0;
}

So he also wrote a procedure, as follows:

Code
#include <stdio.h>
#include <string.h>

int main ()
{
int secvoltagedelta[132];


for (i = 5; i < i++)
{
Secvoltagedelta[i] = i;
}

printf ("Before memcpy! \ n ");
for (i = 0; i < i++)
{
printf ("%d", secvoltagedelta[i]);
}


memcpy ((void*) &secvoltagedelta, (void*) &secvoltagedelta[128], 4);

printf ("\nafter memcpy!\n");
for (i = 0; i < i++)
{
printf ("%d", secvoltagedelta[i]);
}
printf ("\ n");
}

Originally thought own this program to be able to copy the last 4 numbers of the array to the front, but the result of the run only secvoltage[128] The value copy to the first, thought for a long time did not want to understand.

Then carefully read the description of the memcpy function, and finally found:

Function: the memory area referred to by SRC is copied from count bytes to the memory area of Dest.
is to copy count bytes, just as int is 4 bytes, so only one number is copied.

Reprint please specify the original link, http://www.cnblogs.com/flyingcloude/p/6992215.html

memcpy usage of C language

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.