Summarize C and C++ questions about string Pointers that you might encounter in an interview

  • 2020-05-12 02:54:00
  • OfStack

preface

Do not know you have this kind of experience, a lot of interview questions seem simple, but need deep basic skills to give a perfect answer. The enterprise asks the interviewer to write a simple strcpy function, which can tell how much the interviewer has achieved technically. Can we really write a good strcpy function? We all think we can, but the strcpy we wrote is probably only going to get 2 out of 10. You can see in this article an example of how the strcpy function breaks down answers from 2 to 10 to see where you fit in. In addition, there are some interview questions to test the candidates' quick thinking ability.

Analysis of these interview questions, itself contains a strong interest; As a research and development personnel, through the in-depth analysis of these interview questions can further enhance their own internal work.

Looking for a wrong topic

Item 1:

Here is the quote:


void test1() 
{ 
  char string[10]; 
  char* str1 = "0123456789"; 
  strcpy(string, str1); 
} 

Item 2:

Here is the quote:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 

Item 3:

Here is the quote:


void test3(char* str1) 
{ 
  char string[10]; 
  if(strlen(str1) <= 10 ) 
  { 
   strcpy(string, str1); 
  } 
} 

Answer:

Test 1: the string str1 needs 11 bytes to store (including the '\0' at the end), while string has only 10 bytes of space.

For question 2, 3 points are awarded if the interviewer indicates that the character array str1 does not end in the array; If the interviewer points it out strcpy(string,str1) The call makes the number of bytes copied from str1 memory to string memory uncertain and gives a score of 7. On this basis, it points out that the library function strcpy works and gives a score of 10.

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

Analysis:

How to master the basic skills:

(1) the string ends with '\0';

(2) the sensitivity to grasp the array beyond bounds;

(3) the library function strcpy works. If the total score of a standard strcpy function is 10, the following answers are given for several different scores:

Two points

Here is the quote:


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

Four points

Here is the quote:


void strcpy( char *strDest, const char *strSrc ) 
//  Adds the source string const , indicating that it is an input parameter, plus 2 points  
{ 
   while( (*strDest++ = * strSrc++) !=  ' \0' ); 
} 

7 points

Here is the quote:


void strcpy(char *strDest, const char *strSrc) 
{ 
  // Add a non to the source and destination addresses 0 Assertions, 3 points  
   assert( (strDest != NULL) &&(strSrc != NULL) ); 
   while( (*strDest++ = * strSrc++) !=  ' \0' ); 
}

10 points

Here is the quote:


//  To achieve the chain operation, return the destination address, plus 3 points ! 
char * strcpy( char *strDest, const char *strSrc ) 
{ 
   assert( (strDest != NULL) &&(strSrc != NULL) ); 
   char *address = strDest; 
   while( (*strDest++ = * strSrc++) !=  ' \0' ); 
   return address; 
} 

From the answers given from 2 to 10 points, we can clearly see that little strcpy is hiding so many mysteries. It's really not a cover up! What a solid foundation is required to write a perfect strcpy!

(4) mastery of strlen, which does not include the '\0' at the end of the string.

After reading the strcpy version with different scores, you should be able to write a 10-point strlen function. The perfect version is:

Here is the quote:


int strlen( const char *str ) // The input parameters const 
{ 
  assert( strt != NULL ); // Asserts that the string address is not 0 
   int len; 
   while( (*str++) != '\0' ) 
   { 
   len++; 
   } 
   return len; 
} 

Item 4:

Here is the quote:


void GetMemory( char *p ) 
{ 
  p = (char *) malloc( 100 ); 
} 
 
void Test( void ) 
{ 
  char *str = NULL; 
  GetMemory( str ); 
  strcpy( str, "hello world" ); 
  printf( str ); 
} 

Item 5:

Here is the quote:


char *GetMemory( void ) 
{ 
  char p[] = "hello world"; 
  return p; 
} 
 
void Test( void ) 
{ 
  char *str = NULL; 
  str = GetMemory(); 
  printf( str ); 
} 

Item 6:

Here is the quote:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
0

Item 7:

Here is the quote:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
1

Answer:

Test 4 coming in GetMemory( char *p ) The parameter of the function is a string pointer. Modifying the parameter inside the function does not really change the value of the parameter passed in


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
2

str is still NULL;

Item 5


char p[] = "hello world"; 
return p; 

The p[] array is a local automatic variable in the function. After the function returns, the memory has been freed. This is a common mistake that many programmers make, rooted in not understanding the lifetime of variables.

GetMemory of test 6 avoids the problem of test 4 by passing in a pointer to the string pointer as the parameter of GetMemory, but executing the request memory and assignment statement tiffanybracelets in GetMemory


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
4

If the application is not judged to be successful, the following should be added:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
5

The memory of malloc is not freed in the Test function of test 6.

Question 7 has the same problem as question 6 in implementation


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
6

After the memory did not make a judgment whether the application was successful; In addition, in free(str) str is not left empty, which may become a "wild" pointer. Add:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
7

Analysis:

Test questions 4 ~ 7 test the interviewer's understanding of memory operation, the solid basic skills of the interviewer 1 can correctly answer one of the 50~60 errors. But getting it all right is no easy task.

The examination of memory operation mainly focuses on:

(1) pointer understanding;

(2) the survival period and scope of the variables;

(3) good dynamic memory application and release habits.

Let's see what's wrong with the following one:

Here is the quote:


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 area and cause the program to crash. In VC++, the DEBUG runtime prompts an error "Access Violation". The procedure should be changed to:

Here is the quote:


void test2() 
{ 
   char string[10], str1[10]; 
  int i; 
  for(i=0; i<10; i++) 
   { 
   str1= 'a'; 
  } 
  strcpy( string, str1 ); 
} 
9

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: