double strtod(const char *str, char **endptr);
The argument endptr is a pointer to a pointer. The address of the character that stopped the scan is stored in the pointer that endptr points to.
why bother using (char **endptr) instead of (char ch) or (char *chptr) in the function strtod().
Thanks.
Because that argument is meant to give information to the caller. So it must modify memory that the caller controls, and that is typically and most easily done by passing the address of a variable in the caller.
One could have an argument of type
char*that points to a singlecharvariable in the caller, but that would not allow an easy check of whether the result should be accepted (e.g. if trailing whitespace is to be ignored) or if further processing of the remainder of the input string is desired.Having the type
char**allows usingstrtoXin parsers quite comfortably, they leave a pointer to the remainder of the string after they have consumed their part.If the type were
char*, and only thecharafter the partstrtoXconsumed were saved, it would require locating the point of further parsing by essentially re-parsing the number.