Search code examples
cstrtod

strtod definition and type of passed pointer


From definition double strtod ( const char * str, char ** endptr );

C reference sites provide an example for that mighty function:

char szOrbits[] = "365.24 29.53";
char * pEnd;
double d1, d2;
d1 = strtod (szOrbits,&pEnd);
d2 = strtod (pEnd,NULL);

If endptr should be of type char ** why is char *pEnd used here ? And not char **pEnd ?


Solution

  • The type of pEnd is char *. The type of &pEnd is char **.

    C passes arguments to functions by value, so to modify a pointer you need to pass a pointer to the pointer.

    You could define a char ** directly but you would need to initialize it to a char * as the idea is that strtod is modifying a char *.

    Like:

    char *q;
    char **p = &q;
    
    d1 = strtod (szOrbits, p);
    

    Obviously the intermediate p pointer is not necessary and you can use &q directly.