InC/C ++In the interview process of programmers, many interview questions seem simple, but they need profound basic skills to give a perfect answer. Enterprises require the interviewer to write the simplest strcpy function to see how technically the interviewer has achieved. Can we really write a strcpy function? We all think we can, but the strcpy we write may only get 2 out of 10 points. In this article, you can see examples of strcpy functions from 2 to 10 to see what level they belong.
Locate the wrong question
Question 1:
- The following is a reference clip:
- Void test1 ()
- {
- Char string [10];
- Char * str1 = "0123456789 ";
- Strcpy (string, str1 );
- }
Question 2:
- The following is a reference clip:
- Void test2 ()
- {
- Char string [10], str1 [10];
- Int I;
- For (I = 0; I <10; I ++)
- {
- Str1 = 'a ';
- }
- Strcpy (string, str1 );
- }
Question 3:
- The following is a reference clip:
- 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
- The following is a reference clip:
- Void strcpy (char * strDest, char * strSrc)
- {
- While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
- }
4 points
- The following is a reference clip:
- 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
- The following is a reference clip:
- 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
- The following is a reference clip:
- // 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!
(4) Master strlen, which does not include '\ 0' at the end of the string '.
After reading strcpy versions with different scores, you can write a strlen function with 10 points. The perfect version is int strlen (const char * str). // enter the const parameter.
Question 4:
- The following is a reference clip:
- Void GetMemory (char * p)
- {
- P = (char *) malloc (100 );
- }
- Void Test (void)
- {
- Char * str = NULL;
- GetMemory (str );
- Strcpy (str, "hello world ");
- Printf (str );
- }
Question 5:
- The following is a reference clip:
- Char * GetMemory (void)
- {
- Char p [] = "hello world ";
- Return p;
- }
- Void Test (void)
- {
- Char * str = NULL;
- Str = GetMemory ();
- Printf (str );
- }
Question 6:
- The following is a reference clip:
- Void GetMemory (char ** p, int num)
- {
- * P = (char *) malloc (num );
- }
- Void Test (void)
- {
- Char * str = NULL;
- GetMemory (& str, 100 );
- Strcpy (str, "hello ");
- Printf (str );
- }
Question 7:
- The following is a reference clip:
- Void Test (void)
- {
- Char * str = (char *) malloc (100 );
- Strcpy (str, "hello ");
- Free (str );
- ... // Other omitted statements
- }
Answer:
In question 4, the form parameter of the GetMemory (char * p) function is a string pointer. Modifying the form parameter within the function does not actually change the value of the input form parameter.
Char * str = NULL;
GetMemory (str );
The subsequent str is still NULL;
Question 5
Char p [] = "hello world ";
Return p;
The p [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.
GetMemory in question 6 avoids Question 4. The input parameter of GetMemory is a pointer to the string pointer, but the request memory and the value assignment statement tiffanybracelets is executed in GetMemory.
* P = (char *) malloc (num );
If the memory application is successful, add:
If (* p = NULL)
{
... // Handle the Memory Request failure
}
Question 7 has the same problem as question 6.
Char * str = (char *) malloc (100 );
In addition, after free (str), str is not set to null, which may become a "wild" pointer. The following should be added:
Str = NULL;
In the Test function of question 6, the memory of malloc is not released.
Analysis:
Question 4 ~ 7. Measure the test taker's understanding about memory operations. Generally, 50-50 of the respondents with solid basic skills can answer these questions correctly ~ 60 error. However, it is not easy to answer the correct questions completely.
The memory operations are mainly focused on:
(1) pointer understanding;
(2) The survival time and scope of the variable;
(3) good dynamic memory application and release habits.
Let's take a look at the following program errors:
- The following is a reference clip:
- Swap (int * p1, int * p2)
- {
- Int * p;
- * P = * p1;
- * P1 = * p2;
- * P2 = * p;
- }
In the swap function, p is a "wild" pointer, which may point to the system zone, causing program running to crash. In VC ++, the "Access Violation" error is prompted during DEBUG ". The program should be changed:
- The following is a reference clip:
- Swap (int * p1, int * p2)
- {
- Int p;
- P = * p1;
- * P1 = * p2;
- * P2 = p;
- }
In short, the interview questions of C/C ++ programmers not only examine the knowledge level of the subject, but also examine the agile Thinking Ability of the subject. As a R & D Engineer, through in-depth analysis of these interview questions, you can further enhance your internal strength.
Visual C ++ development practices 1200 (Volume I)
C ++ exploring: 68 lectures on C ++
C # debugging from entry-level to proficient