some strange strtod behaviour, please help
O. Hartmann
ohartman at uni-mainz.de
Fri Jul 7 09:08:04 UTC 2006
Hello out here,
sorry bothering you with some standard issues ...
I ran into trouble with a routine from <string.h>.
Trying to convert strings tokenized via strsep() to doubles lead me into
unsolvable problems. This is an example piece of code taken from an
exercise page (my own code looks similar, but this simple code reveals
also the oddity of strtod or my oddity in thoughts):
#include <stdio.h>
#include <math.h>
#include <string.h>
void
main(void) {
char *s,**p;
float dx;
int i;
s = (char *) malloc(256 * sizeof(char));
strncpy(s,"1.234y",strlen("1.234y"));
p = NULL;
dx = strtod(s,p);
i = 0,
printf("String is: %s, Double is %#.G.\n",s,dx);
printf("s[%i] is %c.\n",i,s[i]);
}
On my box (FreeBSD 6.1-STABLE, Intel i386, P4 3.0GHz, HTT enabled) at
lab, compiling this with gcc (standard settings as taken from make.conf)
results in weird behaviour, this also occurs on my home's box (also
FreeBSD 6.1-STABLE, but AMD64).
I also tried to give **p memory via malloc to see to what it points
after strtod() has been called, but not success.
The odd thing is, that I always given bad results like this:
./test
String is: 1.234y, Double is -9.E+08.
s[0] is 1.
The row "printf("s[%i] is %c.\n",i,s[i]);" is only for some testing
purposes to see what the initial portion of the string that is about to
be converted may be.
Ok, maybe I misunderstodd things. As I read the manpage, a string
pointed to by *s containing digits, exponential sign and + or - is
converted into a double returned by strtod(). If **p isn't the NULL
pointer, it points to the first character couln't be a part of a number.
In other words, I do not need to know what it is if s points to a legal
number like in the example above.
What is going wrong?
Thanks in advance a lot,
Oliver
More information about the freebsd-questions
mailing list