Difference between strcpy and strncpy

Source: Internet
Author: User

Today, chinsoft International has come to the school for a written test and has taken this test. Unfortunately, I didn't get a correct answer, so I found some information and looked at it. Summary:

First case:
Char * p = "how are you? ";
Char name [20] = "ABCDEFGHIJKLMNOPQRS ";

 

Strcpy (name, p); // change the name to "how are you? OPQRS "===> error!
Strncpy (name, p, sizeof (name) // change the name to "how are you? "====> Correct!

Case 2:
Char * p = "how are you? ";
Char name [20];

Strcpy (name, p); // change the name to "how are you? Unknown character "===> error!
Name [sizeof (name)-1] = '"0' // combine it with the previous step to get the correct result!
Strncpy (name, p, sizeof (name); // change the name to "how are you? "====> Correct!

Case 3:
Char * p = "how are you? ";
Char name [10];

Strcpy (name, p); // name changed to "how are yo" ===> no Terminator '"0', Error!
Name [sizeof (name)-1] = '"0' // combine it with the previous step to make up the result. Note that the character transfer is incorrect!
Strncpy (name, p, sizeof (name); // The result is the same as the one-step strcpy!

 

Conclusion: strcpy
If the source length is greater than the target length, the source length is medium to the target length.
If the source length is <the target length, the source length is all copied to the target string, excluding '"0'
Strncpy
If the target length is greater than the specified length, all the source lengths are copied to the target length, and '"0' is automatically added'

If the length is specified, copy the source length to the target string, excluding '"0'

If the length is specified> the target length, error happen!

Memcpy strcpy strncpy lstrcpy lstrncpy wstrcpy, memmove

Memcpy
Prototype extern void * memcpy (void * dest, void * src, unsigned int count );
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.
Error may occur:
1. the dest buffer is not large enough to accommodate the src content.
2. The value of count is greater than the src buffer length, causing the copy to be out of bounds to other content.
3. Memory overlaps between dest and src.
For example, array [] = {0, 1, 2, 3, 4}
/* Convert array to {, 0} Instead of {, 2 }*/
Memcpy (& array [2], & array [0], 3 );

Strcpy
Prototype: extern char * strcpy (char * dest, char * src );
Function: Copies the string ending with NULL indicated by src to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and the dest must have sufficient space to accommodate the src string. Returns the pointer to dest.
Error may occur:
1. src and dest overlap.
2. Since dest space is insufficient, it can hold the src string
3. Use the previously initialized Array
Char * p = "I love pizza! ";
Char name [20] = "ABCDEFGHIJKLMNOPQRS ";
Strcpy (name, p); // name becomes "I love pizza! OPQRS "error!
4. The string contains Chinese characters, which take up two bytes.
Char * p = "ABCDEFGHIJKLMNOPQR ";
Strcpy (name, p );

Strncpy
Prototype: extern char * strncpy (char * dest, char * src, int n );
Function: copy the first n Bytes of the string ending with NULL in src to the array indicated by dest.
Note:
If the first n Bytes of src do not contain NULL characters, the result will not end with NULL characters.
If the src length is less than n Bytes, fill the dest with NULL until the n Bytes are completely copied.
The memory areas specified by src and dest cannot overlap and dest must have enough space to hold src strings.
Returns the pointer to dest.
Error may occur:
1. src and dest overlap
2. dest is insufficient to hold src strings.
3. The security is higher than strcpy, but when the dest length is greater than the src length, the two errors are the same. String truncation, no "0.

Lstrcpy lstrncpy
Prototype: LPTSTR lstrcpy (LPTSTR dest, LPTSTR src );
LPTSTR lstrcpy (LPTSTR dest, LPTSTR src, int count );
Function: similar to strcpy and strncpy
Note: This is a windows API and can only be used on windows platforms. The difference between strcpy and strncpy is that if unicode is defined, lstrcpy () is changed to wstrcpy () to copy double-byte unicode characters.

Wstrcpy
Prototype: wchar_t * wstrcpy (wchar_t * ws1, wchar_t * ws2 );
Wchar_t * wstrncpy (wchar_t * ws1, wchar_t * ws2, int n );
Function: similar to strcpy and strncpy, but applies to wide characters,
Note: The header file is <wstring. h>, not <string. h>.
Error may occur:
Similar to narrow character copy

Memmove
Prototype: extern void * memmove (void * dest, const void * src, unsigned int count );
Function: copy count bytes from the memory area indicated by src to the memory area indicated by dest.
Note: src and dest indicate that the memory areas can overlap, but the src content will be changed after replication. The function returns a pointer to dest. It can be seen that memmove is much safer than memcpy!

 

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.