Memcpy function usage

Source: Internet
Author: User

Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # include <string. h>
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
Example:

 
# 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 ;}

See the following code:

 
Void * memcpy (void * DEST, void * SRC, unsigned int count) {assert (DEST! = NULL) & (SRC! = NULL); If (DEST = SRC) return SRC; char * D = (char *) DEST; char * s = (char *) SRC; while (count --> 0) * D ++ = * s ++; return DEST ;}

This is the source code of memcpy. You can see that a temporary pointer is generated in the function, so that the original pointer will not be changed.
Function prototype: extern void * memmove (void * DEST, const void * SRC, unsigned int count)
Parameter description: DEST is the destination string, SRC is the source string, and count is the number of bytes to be copied.
Database Name: # include <string. h>
Function: copy the first n bytes in the SRC string to the DeST.
Return Description: the memory areas indicated by Src and DEST can overlap. The Void * type pointer is returned by the function. Www.yueluo.net
Other Instructions: functions are the same as those of memcpy.
The difference is that when SRC and DEST refer to memory areas overlapping, memmove () can still be correctly processed, but the execution efficiency is slightly slower than memcpy ().
Memcpy (), memmove (), and memccpy ()
-------------------------------------------------------
The functions of these three functions are to copy a memory block to another memory block. The difference between the first two functions is that they process overlapping in memory. The function of the third function is to copy the memory, but if a specific value is encountered, the copy will be stopped immediately.
For library functions, because there is no way to know the memory area passed to them, memmove () function should be used. This function ensures that no memory block overlap occurs. For applications, the memcpy () function can be safely used because the code "knows" that two memory blocks do not overlap.
-------------------------------------------------------

 
# Include <string. h> # include <stdio. h> int main () {char s [] = "zengxiaolong"; memmove (S, S + 4, strlen (S)-4); s [strlen (s) -4] = '\ 0'; printf ("* s = % s \ n", S); Return 0 ;}

Differences Between memcpy and strncpy
strncpy copies num characters from SRC to DeST. However, if the end of the SRC character is reached, the replication ends ahead of schedule, and no subsequent characters are copied, do not process it. Of course, DEST and SRC addresses cannot overlap.
memcpy also copies num characters from SRC to DEST, but it is memory replication, whether it is null or not, still eat all

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.