Rewrite appropiate programs from earlier chapters and exercises with pointers instead of array indexing. Good posibilities include
getline
(Chapter 1 and 4),atoi
,itoa
, and their variants(Chapters 2, 3, and 4),reverse
(Chapter 3), andstrindex
andgetop
(Chapter 4)
Here is my solution:
static char *hidden(int n, char *s) {
char *p;
if(n / 10) {
p = hidden(n / 10, s);
}
else {
p = s;
}
*p++ = n % 10 + '0';
*p = '\0';
return p;
}
char *itoa(int n, char *s) {
if(n < 0) {
*s = '-';
hidden(-n, s + 1);
}
else {
hidden(n, s);
}
return s; /* pointer to first char of s*/
}
itoa
is a wrapper for the function hidden
. The function hidden
returns a pointer to the next free slot in the array s
. At the deepest level of recursion it returns a pointer to the first element (the else branch) of the array.
hidden
function. It's hard to tell what the function does. \$\endgroup\$ – Brennan Vincent Feb 5 '14 at 15:25