From owner-freebsd-questions@FreeBSD.ORG Fri Jul 7 09:08:04 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7E4F816A4DD for ; Fri, 7 Jul 2006 09:08:04 +0000 (UTC) (envelope-from ohartman@uni-mainz.de) Received: from mailgate2.zdv.Uni-Mainz.DE (mailgate2.zdv.Uni-Mainz.DE [134.93.178.130]) by mx1.FreeBSD.org (Postfix) with ESMTP id 160CB43D45 for ; Fri, 7 Jul 2006 09:08:03 +0000 (GMT) (envelope-from ohartman@uni-mainz.de) Received: from [130.133.86.198] (telesto.geoinf.fu-berlin.de [130.133.86.198]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mailgate2.zdv.Uni-Mainz.DE (Postfix) with ESMTP id 3CF0D3003A77 for ; Fri, 7 Jul 2006 11:08:02 +0200 (CEST) Message-ID: <44AE246E.9090002@uni-mainz.de> Date: Fri, 07 Jul 2006 11:07:58 +0200 From: "O. Hartmann" User-Agent: Thunderbird 1.5.0.4 (X11/20060606) MIME-Version: 1.0 To: freebsd-questions@freebsd.org Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at uni-mainz.de Subject: some strange strtod behaviour, please help X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Jul 2006 09:08:04 -0000 Hello out here, sorry bothering you with some standard issues ... I ran into trouble with a routine from . 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 #include #include 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