About strcpy parsing?

Source: Internet
Author: User

1. First, let's look at the implementation of the strcpy function in the kernel for one year:

The parameters of the strcpy () function are pointers to the destination string and the source string,

The function is to pass the source string to the pointer that stores the destination string.

Char * strcpy (char * DEST, const char * SRC)

{// TMP stores the first address that points to the target string. When you return the result, the returned address is the address of the copied string.
Char * TMP = DEST;

// The copy process. If the source string accessed by one operation is not an Terminator, the copy process will continue.
While (* DEST ++ = * SRC ++ )! = '\ 0 ')
/* Nothing */;
Return TMP; // returns the pointer to the Dest string.

}


2. Differences and Similarities between strncpy and strcpy.

Char * strncpy (char * DEST, const char * SRC, size_t count)
{// TMP stores the first address that points to the target string. When you return the result, the returned address is the address of the copied string.
Char * TMP = DEST;

// Count indicates the number of copies.
While (count ){
If (* TMP = * SRC )! = 0)
SRC ++;
TMP ++;
Count --; // The number of copies to be copied minus 1 per backrest
}
Return DEST; // return the first address that points to the Dest string.
}

Char * strncpy refers to the string that has been copied to be returned.

This function is very easy to implement in the kernel, but many interviewers prefer to ask this question during the interview,

Note: Why should I add a char * to the return type and a char * To accept the return type?

.




About strcpy parsing?

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.