It's all a fault caused by '\ 0' -- a detailed explanation of ZTE's string-related pen questions

Source: Internet
Author: User

0. Introduction

On Sunday afternoon, I participated in ZTE's written examination (for details about the written examination, see my previous blog post "ZTE's written examination return Summary (2012 software)"). Among them, there is a question like this:

Find the followingProgramError

Void test (char * str1)

 1   Void  Test (  Char     *  Str1)
2 {
3 Char String [ 10 ];
4 If (Strlen (str1) <= 10 )
5 {
6 Strcpy ( String , Str1 );
7 }
8 }

At first glance, this function does not have any practical usage (in fact, it is not an error ), even if you do not carefully compile and run the program using VC, you will not find any problems.
After reading this function, we can find that the following two points are worth pondering:
① The real parameter input by char * str1 may be a pointer or an array. This is precisely because of this, the following problems may occur;
② Will strlen () be different for input (pointer or array;
Next we will analyze the above 3.1 items to find out the real "murderer ".

1. pointer as a form parameter

as we all know, when a pointer acts as a form parameter, its real parameter may be a pointer or an array, you may think that the pointer and function are equivalent at this point. but is it actually equivalent? using pointers as input functions of input parameters and arrays as input arrays of input parameters is completely equivalent. Is there any difference between them? I think you may think wrong. There is only one saying: when used as a form parameter, pointers and arrays are equivalent, that is, the function uses
void test ( char * Str)
and
void test ( char STR [])
these two forms mean the same, with no difference.

A little farther away, let's get down to the truth. After the above statements, you may not believe them very much. So let's do an experiment, it proves whether the input pointer to a function is different from the input array.

View test code

 1 # Include <stdio. h>
2 # Include < String . H>
3
4 Void Test2 ( Char * Str1, Char * Str2 );
5
6 Int Main ()
7 {
8 Char * Str1 = " Abcdefgh " ;
9 Char Str2 [ 8 ] = " Abcdefgh " ;
10 Test2 (str1, str2 );
11 Return 0 ;
12 }
13
14 Void Test2 ( Char * Str1, Char * Str2)
15 {
16 Printf ( " Str1: % s \ r \ nstr2: % s \ r \ n " , Str1, str2 );
17 }

the result of the above test is
str1: abcdefgh
str2: abcdefgh (B
it turns out they are different, according to our previous understanding, the printed results should be the same. How can str2 be followed by garbled characters? That is to say, the length of str2 should have been the same as that of str1. Why? Leave this question here first, and you may be clear after reading the following.

Ii. String Length FunctionStrlen ()
String Length Measuring Function strlen is a function in the standard library string. H. Its return value is the actual length of the string.Not includedThe string end flag '\ 0' is included. When you test the length of a string, when you encounter the string end flag' \ 0', the string is deemed to have been tested, this explains why the returned value of the string "ABC \ 0abc" is 3.
Next, let's go back to the example above, regardless of % s, strlen, and some other functions about strings, they all have a great relationship with '\ 0, so it must have been '\ 0' lost for some reason, and then printed to another' \ 0', so the above situation occurs.

Iii. Summary
The above also mentioned the main points of the question at the beginning. Back to the question analysis, when the input string format is str [10] = "abcdeabcde ", in the test function, strlen (STR) has a value greater than 10 and does not meet the if condition. strcpy is not executed. If the input string is in the form of * STR = "abcdeabcde ", the length is exactly 10. If the condition is met, strcpy is executed. In this way, different execution results occur when the strings are the same, which is the error of this program.

Why? When the input is in the form of an array, it is passed to the test function only "abcdeabcde", but not '\ 0'. This naturally causes problems in strlen, if you change the input string to the following format:
STR [11] = "abcdeabcde ";
In this way, you can execute strcpy. You can try it. Because this string is equivalent to "abcdeabcde \ 0", strlen can measure the length.

It is hard to understand why some programmers manually add the character '\ 0' at the end of the string during programming, so there will be no such potential problems.

The above are just some of my superficial understandings. If you have any understanding that doesn't mislead everyone, I hope you can give me some further advice.

Author: Yun Source: Yun Article The original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

0. Introduction

On Sunday afternoon, I participated in ZTE's written examination (for details about the written examination, see my previous blog post "ZTE's written examination return Summary (2012 software)"). Among them, there is a question like this:

Locate the following program errors

Void test (char * str1)

1   Void  Test (  Char     *  Str1)
2 {
3 Char String [ 10 ];
4 If (Strlen (str1) <= 10 )
5 {
6 Strcpy ( String , Str1 );
7 }
8 }

At first glance, this function does not have any practical usage (in fact, it is not an error ), even if you do not carefully compile and run the program using VC, you will not find any problems.
After reading this function, we can find that the following two points are worth pondering:
① The real parameter input by char * str1 may be a pointer or an array. This is precisely because of this, the following problems may occur;
② Will strlen () be different for input (pointer or array;
Next we will analyze the above 3.1 items to find out the real "murderer ".

1. pointer as a form parameter

as we all know, when a pointer acts as a form parameter, its real parameter may be a pointer or an array, you may think that the pointer and function are equivalent at this point. but is it actually equivalent? using pointers as input functions of input parameters and arrays as input arrays of input parameters is completely equivalent. Is there any difference between them? I think you may think wrong. There is only one saying: when used as a form parameter, pointers and arrays are equivalent, that is, the function uses
void test ( char * Str)
and
void test ( char STR [])
these two forms mean the same, with no difference.

A little farther away, let's get down to the truth. After the above statements, you may not believe them very much. So let's do an experiment, it proves whether the input pointer to a function is different from the input array.

View test code

 1 # Include <stdio. h>
2 # Include < String . H>
3
4 Void Test2 ( Char * Str1, Char * Str2 );
5
6 Int Main ()
7 {
8 Char * Str1 = " Abcdefgh " ;
9 Char Str2 [ 8 ] = " Abcdefgh " ;
10 Test2 (str1, str2 );
11 Return 0 ;
12 }
13
14 Void Test2 ( Char * Str1, Char * Str2)
15 {
16 Printf ( " Str1: % s \ r \ nstr2: % s \ r \ n " , Str1, str2 );
17 }

the result of the above test is
str1: abcdefgh
str2: abcdefgh (B
it turns out they are different, according to our previous understanding, the printed results should be the same. How can str2 be followed by garbled characters? That is to say, the length of str2 should have been the same as that of str1. Why? Leave this question here first, and you may be clear after reading the following.

Ii. String Length FunctionStrlen ()
String Length Measuring Function strlen is a function in the standard library string. H. Its return value is the actual length of the string.Not includedThe string end flag '\ 0' is included. When you test the length of a string, when you encounter the string end flag' \ 0', the string is deemed to have been tested, this explains why the returned value of the string "ABC \ 0abc" is 3.
Next, let's go back to the example above, regardless of % s, strlen, and some other functions about strings, they all have a great relationship with '\ 0, so it must have been '\ 0' lost for some reason, and then printed to another' \ 0', so the above situation occurs.

Iii. Summary
The above also mentioned the main points of the question at the beginning. Back to the question analysis, when the input string format is str [10] = "abcdeabcde ", in the test function, strlen (STR) has a value greater than 10 and does not meet the if condition. strcpy is not executed. If the input string is in the form of * STR = "abcdeabcde ", the length is exactly 10. If the condition is met, strcpy is executed. In this way, different execution results occur when the strings are the same, which is the error of this program.

Why? When the input is in the form of an array, it is passed to the test function only "abcdeabcde", but not '\ 0'. This naturally causes problems in strlen, if you change the input string to the following format:
STR [11] = "abcdeabcde ";
In this way, you can execute strcpy. You can try it. Because this string is equivalent to "abcdeabcde \ 0", strlen can measure the length.

It is hard to understand why some programmers manually add the character '\ 0' at the end of the string during programming, so there will be no such potential problems.

The above are just some of my superficial understandings. If you have any understanding that doesn't mislead everyone, I hope you can give me some further advice.

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.