strcpy and strncpy usage and differences

Source: Internet
Author: User

1. strcpy function:

As the name implies string copy function: Prototype: extern char *strcpy (char *dest,char *src);

Function: Assigns a string that starts from the SRC address and contains a null terminator to the address space beginning with dest, returning dest (the address is stored as a new value after the copy).

requirement: SRC and dest memory areas cannot overlap and dest must have enough space to accommodate SRC strings.
General function Prototype implementation method:
char * strcpy (char * strdest,const char * strsrc)
{
char * strdestcopy=strdest;//[3]
if ((strdest==null) | | | (Strsrc==null)) [1]
throw "Invalid argument (s)";//[2]
while ((*strdest++=*strsrc++)! = ' + ');//[4]
return strdestcopy;
}
The function's arguments are character pointers, which can be string variables and character arrays, because their variable names represent the first-character addresses. The string default has a null terminator, and the character array is not. So here's a note: Because SRC requires a null terminator, the length of the character array must be greater than or equal to the total length of the src containing the null terminator. For example, char* src= "ABCD"; Char dest[5]; The length of the dest here is at least 5.
2. strncpy function: Multiple n means you can specify the number of characters to assign.  Prototype: char * strncpy (char *dest, char *src, SIZE_TN); Function: copies up to n characters in the string src to the character array dest (it does not stop copying like strcpy, but waits for n characters to start copying), and returns a pointer to Dest. Requirements: If n > Dest string length, the dest stack space overflow causes a crash exception. The function is similar to strcpy, but the value of n should be paid special attention,
1) Src string length <=dest string length, (the string length here contains the string end of the null character)
if n= (0, src string length), the first n characters of Src are copied into dest. However, because there are no null characters, there is a stack overflow exception to accessing the Dest string directly. At this point, it is generally recommended to take memset to fill all elements of dest with NULL, such as: Memset (dest,0,7) (7 to fill dest from the beginning of the Null,dest start address 7 places can be a character pointer and array name). Note: char* pc= "abc"; Char chs[5]; sizeof (PC) is 4 (contains null) (some compilers do not), and sizeof (CHS) is 5.
If n = src string length, it is consistent with strcpy.
If n = dest string length, [0,src string length] is stored in desk string, (src string length, dest string length] place null.
2) Src string length >dest string length
if n =dest string length, then the dest string does not have a null character, which causes the output to be garbled. If you do not consider the integrity of SRC string replication, you can place the last character of dest as null.
Therefore, n is generally set to dest (with null) length (unless multiple src is copied into dest). When the N=dest string length is in 2), the definition dest is a character array because there is no null character copy at this time.


The difference between strcpy,strncpy and memcpy and how to use it

strcpy , strncpy, memcpy these three C language functions we in the host code writing will be very frequent use, but three functions of the difference, the use of the attention of what is necessary to say.

This article is written in reference to the C standard library.

First, function description

1 , memcpy functions

void *memcpy (void *s1, const void *S2, size_t n);

Description

The memcpy function copies n characters from an object pointed to by S2 to the object that S1 points to. If replication occurs in two overlapping objects, this behavior is undefined.

return value:

The function memcpy Returns the value of S1.

2 , strcpy functions

Char *strcpy (char *s2, const char *S1);

Description

The function strcpy copies the string (including the null character) pointed to by S1 into the array S2 points to. If replication occurs in two overlapping objects, this behavior is undefined.

return value:

The function strcpy Returns the value of S2.

3 , strncpy functions

Char *strncpy (char *s2, const char *S1, size_t N);

Description

The function strncpy copies up to n characters from the array pointed to by S1 (not copying the characters following the null character) into the array that S2 points to. If replication occurs in two overlapping objects, this behavior is undefined.

if the array that the S1 points to is a shorter string than N, the character is appended to the S2-defined array until n characters are written.

return value:

The function strncpy Returns the value of S2.

Second, the matters needing attention

strcpy is based on "S2" as the end of judgment, if the space is not enough, it will cause buffer overflow.

memcpy used to copy data in memory, because the stringends with "\", the data containing "memcpy" can only be used in the data.

Strncpy Similar to memcpy , except that it stops at a terminating null character. When N>strlen (S1), to S2 not enough space to fill the "" ", when N<=strlen (S1), S2 is no terminator" "".

This hides the fact that the memory that S2 points to must be written in n characters.

So generally note:

1 , S2 point to a sufficient amount of space to copy; When using strcpy, the S2 points to a space greater than or equal to the S1 point, and when strncpy or memcpy is used, the S2 points to more than or equal to N.

2 , when using strncpy or memcpy, n should be greater than strlen (S1), or preferably n >= strlen (S1) +1; This 1 is the last "."

3 , when using strncpy , make sure that the last character of the S2 is "."



strcpy and strncpy usage and differences

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.