Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 07 Jul 2006 11:07:58 +0200
From:      "O. Hartmann" <ohartman@uni-mainz.de>
To:        freebsd-questions@freebsd.org
Subject:   some strange strtod behaviour, please help
Message-ID:  <44AE246E.9090002@uni-mainz.de>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?44AE246E.9090002>