I have a issue with my number to string implemention. For some reason I keep getting 000 on my terminal even though. I couldn't find a solution, what is the potantial issue here? Im now sure my code is broken but don't really see the problem.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* public domain code feel free to use as what you wish, no warranties given*/
char finddigits(unsigned n) {
char base = 6;
unsigned tester = 100000;
while(base % tester != 0) {
base--;
/* inefficient but works for now */
switch(tester) {
case 100000:
tester = 10000;
break;
case 10000:
tester = 1000;
break;
case 1000:
tester = 100;
break;
case 100:
tester = 10;
break;
case 10:
tester = 1;
break;
}
}
return base;
}
char* num2str(unsigned n) {
char size = finddigits(n);
char* tempbuf = malloc(size);
*tempbuf = 48 + (n / pow(10, size));
for(unsigned int i = 1; i < size; i++)
*(tempbuf + i) = 48 + (n % (10 * i));
return tempbuf;
}
int main(int argc, char* argv[]) {
int numbr = 210;
printf("%s \n", num2str(numbr));
/* expected 210 on string got 000 */
return 0;
}