An Implementation Of Itoa
Oct 2, 2014 at 1:49pm UTC
the following implementation i found on the internet on a reliable source have to work
i don't understand a specific row 21 how do you write a string with [ brackets and what does it mean?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
1. /**
2. * C++ version 0.4 char* style "itoa":
3. * Written by Lukás Chmela
4. * Released under GPLv3.
5. */
6.
7. char * itoa_c(int value, char * result, int base)
8. {
9. // check that the base if valid
10. if ( base < 2 || base > 36 ) {
11. *result = '\0' ;
12. return result;
13. }
14.
15. char * ptr = result, *ptr1 = result, tmp_char;
16. int tmp_value;
17.
18. do {
19. tmp_value = value;
20. value /= base;
21. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
22. } while ( value );
23.
24. // Apply negative sign
25. if ( tmp_value < 0 )
26. *ptr++ = '-' ;
27. *ptr-- = '\0' ;
28.
29. while ( ptr1 < ptr ) {
30. tmp_char = *ptr;
31. *ptr-- = *ptr1;
32. *ptr1++ = tmp_char;
33. }
34.
35. return result;
36. }
Oct 2, 2014 at 2:02pm UTC
The string literal "zyx..." is an array of characters. Like with any other array you can access elements by putting an index inside [ ].
Last edited on Oct 2, 2014 at 2:02pm UTC
Oct 2, 2014 at 4:21pm UTC
and *ptr++ = ponits to a sepcific character and then make one addition of the pointer ?
Oct 2, 2014 at 4:53pm UTC
Splitting it over two lines makes it easier to follow.
Last edited on Oct 2, 2014 at 4:54pm UTC
Topic archived. No new replies allowed.