Recursive Implementation of itoa functions (implementation of second-level pointers) and recursive pointer of itoa Functions

Source: Internet
Author: User

Recursive Implementation of itoa functions (implementation of second-level pointers) and recursive pointer of itoa Functions
Problem proposal

In C Programming Language, two functions are reserved for recursion. the itoa function is used to convert an integer into a string. There are already versions implemented using loops in the book, but the reverse result is obtained directly. You need to call the reverse function at last. Recursive versions can avoid this problem.
First, use the original interface void itoa (int n, char s []) for implementation. It is found that recursive calling is always incorrect. The output result can only obtain the maximum and maximum digits of integer n. The Code is as follows:

 1 void itoa(int n, char* s) 2 { 3     if (n < 0) 4     { 5         *s = '-'; 6         s++; 7     } 8     if (abs(n) / 10) 9         itoa(abs(n) / 10, s++);10     *s = abs(n) % 10 + '0';11 }

Basic Ideas: The character pointer s is used for moving, and the pointer auto-increment is performed during recursive calling. However, the running result after gcc compilation is incorrect. Only two numbers with the highest and lowest digits of integer n can be output.
Error cause: After careful debugging, it is found that the character pointer parameter is passed in each recursive call.s++This variable is copied, that is, the auto-increment operation on Pointer s in subsequent recursive calls will not be reflected in Pointer s called for the first time. Therefore, this function is called for the first time and finally called.*s = abs(n) % 10 + '0';In this statement, the pointer s points to the second position of the character array. Place the nth bit of integer n in the second position, while the highest bit of integer n is always recursively called and the parameter pointer variable is copied (always pointing to the first position ), it is placed in the first position. Only two numbers are returned.

Existing solutions

Search for the problem on the Internet. The most commonly used is to use local static variables to save the subscript of the character array. For more information, see http://blog.csdn.net/long_xing/article/details/2212.pdf. However, the implementation in another blog has a logic error. Assign "\ 0" to the character array in advance. For details, see http://blog.csdn.net/roma823/article/details/6546719.

Self-implemented

After a period of thinking, the final thought is the second-level character pointer, because when the character pointer function parameter is used, the passed character pointer is recursively called and copied, this should be the compiler's implementation of the character pointer (equivalent to a string), which uses the copy pointer (that is, the copy string) when passing parameters, it is different from the idea of directly operating the source pointer when calling pointer parameters in the general sense. Therefore, when a second-level character pointer is used, each time it is passed to the same pointer of the string, the auto-increment operation of the pointer in the recursive call process will not be ineffective for the function called at the previous level, the Code is as follows:

 1 void itoa(int num, char** s) 2 { 3     if (num < 0) 4     { 5         **s = '-'; 6         (*s)++; 7     } 8     if (abs(num / 10) > 0) 9     {10         itoa(abs(num / 10), s);11         (*s)++;12     }13     **s = abs(num % 10) + '0';14 }

The above version uses the abs library function to convert the maximum negative integer. The test is as follows:

1 int main () 2 {3 char s [100], * ps = s; 4 itoa (12345, & ps); // test positive integer 5 * (++ ps) = '\ 0'; 6 printf ("% s \ n", s); 7 8 ps = s; 9 itoa (-2147483647, & ps ); // test the maximum negative integer (32 bits) 10 * (++ ps) = '\ 0'; 11 printf ("% s \ n", s ); 12 13 return 0; 14}

 

Related Article

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.