Domanda

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 ?

È stato utile?

Soluzione

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.

Altri suggerimenti

pEnd is a char*, as you've observed. The first example call to strtod() passes &pEnd as the second argument. &pEnd is the address of pEnd, and is therefore of type char**.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top