I'm considering to use the strtod() function to convert a string to a double in C, which siganture is:
double strtod (const char* str, char** endptr);
being str
the string to be converted and endptr
a pointer to be set to to the first character after the number.
Documentation says:
On success, the function returns the converted floating point number as a value of type double. If no valid conversion could be performed, the function returns zero (0.0).
Thus, as far as I understand, it is not possible to detect situations in which str
has a format error (e.g. "foo", "-3ab" or "3o3") from situations in which the str
represents 0 (e.g. "0", "0.000", "-0.0", "0e10").
How strtod() function could be used avoiding that problem in zero conversion?
EDIT: I have seen a similar question here. However, I think I'm not asking the same, as my question is about the ambiguity problem between 0 and wrongly formated string, while that other post is about detecting formating errors in general.