Path: utzoo!attcan!uunet!mcsun!sunic!tut!ra!uwasa.fi!fs From: fs@uwasa.fi (Filip Sawicki LAKE) Newsgroups: comp.lang.c Subject: Re*2: an elementary question concerning double indirection Summary: another solution Keywords: double indirection, strtod Message-ID: <1990Feb27.110204.1415@uwasa.fi> Date: 27 Feb 90 11:02:04 GMT References: <8146@hubcap.clemson.edu> <1458@amethyst.math.arizona.edu> Organization: University of Vaasa Lines: 41 In article <1458@amethyst.math.arizona.edu> raw@math.arizona.edu (Rich Walters) writes: >In article <8146@hubcap.clemson.edu> kaires@hubcap.clemson.edu (Robert G Kaires) writes: >> this function has the syntax: >> double strtod(const char *s, char **endptr); >> [deleted] >A better way might be [deleted] > char string[30]; > char *ptr; > double ans; > extern double strtod(); > > while(1) { > gets(string); > if (*string == 'q') break; > ans=strtod(string,&ptr); > if ( ( string+strlen(string) ) != ptr ) /* <--- warning here */ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ WRONG !!! > printf("format error\n"); > else > printf("You typed the number: %f\n",ans); > } >} > > >This works. > Richard Walter That does not compute - especially when the string is empty (*string=='\0'). According to the SUN manual, strtod returns ptr==string if string is completely inconvertible, otherwise ptr points to the first wrong character. So, checking should be as follows: if (ptr==string || *ptr) printf("format error\n"); else /* *ptr=='\0' - string not empty and all chars were transformed */ printf( ... ); fi.