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 40 41 42 43 44 45
/* Transforms a number to a string * e.g 1234 => "1234" * * Pablo A. Torres Navarrete * 02-10-07 */ # include <stdio.h> # include <malloc.h> # include <math.h> int digcount(int num); // counts the digits of <num> main() { int num, fact, dig, digits, i; char *s; scanf("%d", &num); digits = digcount(num); s = (char *) malloc((digits + 1) * sizeof(char)); // assign just enough space to hold the number and '\0' s += (digits - 1); // go to the last address of the memory space assigned *s-- = '\0'; // mark the end of the string for (i = 0; i < digits; i++) { dig = num % 10; num /= 10; *s-- = '0' + dig; // store digit by digit } printf("\"%s\"\n", ++s); // after the for, s is pointing to the addres before the start of the string } int digcount(int num) /* Calculates the number of digits of the parameter using the * decimal logarithm property: * * N = trunc(log(x), 0) + 1, where N is the number of digits of x * */ { double ans; ans = log10(num); ans = ceil(ans); return (int) ans; }
Refactorings
No refactoring yet !
takayama
October 2, 2007, October 02, 2007 21:22, permalink
you should link "libm" to use mathmatic library.
1
cc 49-simplest-of-mallocs-examples.cc -lm
codelion
October 2, 2007, October 02, 2007 22:39, permalink
1
You need to link to the maths library as pointed out above. You have a statement of the form include<math.h> the function log10, ceil etc are in that file and the compiler needs to know where to get it from.
codelion
October 2, 2007, October 02, 2007 22:40, permalink
You need to link to the maths library as pointed out above. You have a statement of the form include<math.h> the function log10, ceil etc are in that file and the compiler needs to know where to get it from.
Anonymous
October 3, 2007, October 03, 2007 14:07, permalink
You forgot to free(s)
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* Transforms a number to a string * e.g 1234 => "1234" * * Pablo A. Torres Navarrete * 02-10-07 */ # include <stdio.h> # include <malloc.h> # include <math.h> int digcount(int num); // counts the digits of <num> char *num2str(int num); main() { int num; char *s = NULL; scanf("%d", &num); s = num2str(num); printf("\"%s\"\n", s + 1); //s is pointing to the addres before the start of the string free(s); } int digcount(int num) /* Calculates the number of digits of the parameter using the * decimal logarithm property: * * N = trunc(log(x), 0) + 1, where N is the number of digits of x * */ { double ans; ans = log10(num); ans = ceil(ans); return (int) ans; } char *num2str(int num) { int digits, i, dig; digits = digcount(num); s = (char *) malloc((digits + 1) * sizeof(char)); // assign just enough space to hold the number and '\0' s += (digits - 1); // go to the last address of the memory space assigned *s-- = '\0'; // mark the end of the string for (i = 0; i < digits; i++) { dig = num % 10; num /= 10; *s-- = '0' + dig; // store digit by digit } return s; }
Practising for my test tomorrow, just doing some simple malloc stuff.
Could anybody tell me why this doesn't compile? I get this error:
/tmp/ccefZYYN.o: In function `digcount':
itos.c:(.text+0x103): undefined reference to `log10'
itos.c:(.text+0x111): undefined reference to `ceil'
collect2: ld returned 1 exit status
I'm using vim for typing and gcc under Ubuntu for the compilation.