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?