What is the difference between strcpy and memcpy?

Source: Internet
Author: User
What are the differences between strcpy and memcpy? The strcpy () function can only copy strings. The strcpy () function copies each byte of the source string to the destination string. When a NULL character ('\ 0') at the end of a string is encountered, it ends the copy and automatically adds' \ 0' to the end of the target string'

The memcpy () function can copy any type of data. Because not all data ends with NULL characters, you must specify the number of bytes to copy for the memcpy () function. When copying a string, the strcpy () function is usually used. When copying other data (such as a structure), the memcpy () function is usually used. Void * memcpy (char * DEST, const char * SRC, size_t size) { Assert (DEST! = NULL) & (SRC! = NULL )); Char * P = DEST; Char * q = SRC; While (size --> 0) * P ++ = * q ++; Return DEST;

}// Shift n-bit function to the right of the string // write a function to shift n loops of a char string to the right. For example, the original function is "abcdefghi"
// If n = 2, it should be "hiabcdefg"
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define MAX_LEN 20/* void LoopMove (char * src, int n)
{
Int m = strlen (src)-n;
Char temp [MAX_LEN];
Strcpy (temp, src + m) // copy the last n characters first and copy them first.
Strcpy (temp + n, src); // copy the entire string
// * (Temp + strlen (src) = '\ 0'; // Add the ending character
Strcpy (src, temp );
} */Void LoopMove (char * src, int n)
{
Int M = strlen (SRC)-N;
Char temp [max_len];
Memcpy (temp, SRC + m, n); // copy the last n characters of the string to the temp array.
Memcpy (SRC + N, SRC, m); // leave the n characters Blank Based on the source string, then copy the first M characters to a proper position. // SRC has an ending character, so you do not need to add it.
Memcpy (SRC, temp, n); // copy the last n characters to a proper position.
}
Int main ()
{
Char STR [] = "abcdefghi ";
Loopmove (STR, 2 );
Printf ("% s \ n", STR );
Return 0 ;}

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.