First "stringify" the int, then you can use a string pointer for this:
int main()
{
int n = 5678;
char *str_p, str[10];//declare a char pointer, and an array of 10 chars
sprintf(str, "%d", n);//initializes str to {'5','6','7','8','\0'}
str_p = &str[0];//init pointer to start of string, str_p = str; works, too
while(*str_p != '\0')
{//while pointer doesn't point to end of string:
printf("%s\n", str_p);//print string str_p points to
str_p++;//shift pointer, to point to next char in string
}
return 0;
}
The resulting output, as you can see in this codepad is:
5678
678
78
8
Quite easy, really.
The trick is str_p++
. At first, when I was learning about pointers, I found this quite confusing. But it really is quite simple. Think of a pointer as that red plastic square thing on a calendar: you slide it over whatever date it happens to be that day:
MAY:
_____________________________
| | | | | |___| |
| 1 | 2 | 3 | 4 | 5 ||6|| 7 |
|___|___|___|___|___|---|___|
This means it's the sixth of may. Right, if we translate this to a pointer + array in C we'd have something like:
int may[31] = {1,2,3,4,5,6,7,8,9,...};
int *today = &may[5];//zero indexed
The action of sliding the red square thing to the next day (today + 1 == tomorrow) is written, logically, like this:
today++;//or today += 1
This is to say:
today + 1: [] =>[]//shift the red square-thingy
_____________________________
| | | | | | |___|
| 1 | 2 | 3 | 4 | 5 | 6 ||7||
|___|___|___|___|___|___|---|
So if I then were to write:
today--;
the same logic applies, and today points back to 6... go to the store, buy one of these calendars if you feel the need to visualize this...
Suppose you want to change the value that is being pointed to by a pointer? Well, sticking to the same analogy (of a slider over some value), you'd have to use your one hand to hold that slider in place, and with the other hand, you can then work on what's underneath it. In your code this is reflected by the inderection *
operator. Think of it as a pin, to hold the pointer in place while you work on whatever it points to:
(*today)++;//or *today++;, both operators have the same precedence
It really, really, really is that simple... Well, no it's not :) but 9/10 times you're using pointers, this way of thinking works
BTW: using str_p = str;
is, I think more logical, and a tad more efficient, but the compiler probably optimizes both statements to the same thing. Still Here's another codepad, to prove both do the same thing
And now, to end, a more generic approach, that'll work with numbers of variable length, dynamically allocating memory required for the string, and freeing it again:
#include
#include
int main()
{
int i, len, n = 5678;
char *str_p = NULL;
int digitCount(int in);
len = digitCount(n);//how many chars do we need to allocate?
str_p = calloc(len, sizeof(char));//allocate string, calloc inits memory to 0
len--;//avoid printing empty string at the end
sprintf(str_p, "%d", n);//set string in memory
for(i=0;i false)
in /= 10;
++count;//add 1 digit to count
}
return count;
}