David C. Rankin wrote:
Listmates,
I was comparing input handling between perl and c and I ran into a very weird problem with the strtof function in c. What am I doing wrong? strtof doesn't seem to be working like the man page shows. Here is my test bit of code:
#include
#include #include int main(int argc, char *argv[]) { char *endptr, *str, newstr[20]; float val, newval;
if (argc < 2) { fprintf(stderr, "Usage: %s number\n", argv[0]); exit(1); }
str = argv[1]; printf("\nstr = %s\nsizeof str = %d\nstrlen str = %d\n", str, sizeof(str), strlen(str));
strcpy(newstr,argv[1]); printf("\nnewstr = %s\nsizeof str = %d\nstrlen str = %d\n\n", newstr, sizeof(newstr), strlen(newstr));
val = strtof(str, &endptr);
endptr is uninitialized, so it's pointing at some random memory location.
newval = strtof(newstr, &endptr);
if (*endptr != '\0') printf("Additional characters after number: %s\n", endptr);
printf("str:\tstrtof of %s = %f\n", str, val); printf("newstr:\tstrtof of %s = %f\n\n", newstr, newval);
return(0); }
The code compiles without error or warning with: "gcc -o prg/tmp src/tmp.c" Here is the bewildering output:
20:03 Rankin-P35a~/linux/programming/c> ./prg/tmp 250.871
str = 250.871 sizeof str = 4 strlen str = 7
newstr = 250.871 sizeof str = 20 strlen str = 7
str: strtof of 250.871 = 1132125952.000000 newstr: strtof of 250.871 = 1132125952.000000
Obviously I must be missing something very simple, but I can't put my hands on it. Help?
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org