Re*2: an elementary question concerning double indirection

Filip Sawicki LAKE fs at uwasa.fi
Tue Feb 27 22:02:04 AEST 1990


In article <1458 at amethyst.math.arizona.edu> raw at math.arizona.edu (Rich Walters) writes:
>In article <8146 at hubcap.clemson.edu> kaires at 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.



More information about the Comp.lang.c mailing list