Analyze strcpy Functions

Source: Internet
Author: User

 

The analysis of these Interview Questions contains strong interests. Find out the errors:

Question 1:

Void test1 ()

{
Char string [10];
Char * str1 = "0123456789 ";
Strcpy (string, str1 );
}

Question 2:

Void test2 ()

{
Char string [10], str1 [10];
Int I;
For (I = 0; I <10; I ++)
{
Str1 [I] = 'a ';
}
Strcpy (string, str1 );
}

Question 3:

Void test3 (char * str1)

{
Char string [10];
If (strlen (str1) <= 10)
{
Strcpy (string, str1 );
}
}

Answer:

Question 1 string str1 requires 11 bytes to be stored (including '\ 0' at the end), while string only has 10 bytes of space, strcpy will cause the array to cross-border;

For question 2, if the subject points out that the character array str1 cannot end in the array, it can give 3 points; if the subject points out strcpy (string, str1) calling makes the number of bytes replicated from the str1 memory to the string memory uncertain. It can be given 7 points. Based on this, it is pointed out that the strcpy function is working for 10 points;

For question 3, if (strlen (str1) <= 10) should be changed to if (strlen (str1) <10 ), the result of strlen does not count the 1 byte occupied by '\ 0.

Analysis:

Measure the test taker's knowledge about basic skills:

(1) The string ends with '\ 0;

(2) sensitivity to array out-of-bounds control;

(3) how the database function strcpy works. If the total score of a standard strcpy function is 10, the following are several different answers:

2 points

Void strcpy (char * strDest, char * strSrc)
{
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
}

4 points

Void strcpy (char * strDest, const char * strSrc)

// Add the source string to const, indicating that it is an input parameter and adds 2 points

{
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
}

7 points

Void strcpy (char * strDest, const char * strSrc)
{

// Add non-0 assertions to the source and target addresses, and add 3 points

Assert (strDest! = NULL) & (strSrc! = NULL ));

While (* strDest ++ = * strSrc ++ )! = '\ 0 ');

}

10 points

// For chained operation, add 3 points to return the destination address!

Char * strcpy (char * strDest, const char * strSrc)

{
Assert (strDest! = NULL) & (strSrc! = NULL ));
Char * address = strDest;
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
Return address;
}

We can clearly see from two to ten answers that the little strcpy has hidden so many xuanjicang! What a solid basic skill is required to write a perfect strcpy!

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.