Mailinglist Archive: opensuse (2532 mails)
| < Previous | Next > |
Re: [opensuse] strtof problem - what am I doing wrong?
- From: Sam Clemens <clemens.sam1@xxxxxxxxx>
- Date: Sat, 03 May 2008 22:00:18 -0400
- Message-id: <481D18B2.6070000@xxxxxxxxx>
David C. Rankin wrote:
endptr is uninitialized,
so it's pointing at some random memory location.
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |