Confused how to convert from a string to double using strtod() in C++

Sam picture Sam · Apr 16, 2011 · Viewed 11.1k times · Source

If someone could explain how to use the function, that would be great. I don't understand the parameters.

Thanks

Answer

Charles Brunet picture Charles Brunet · Apr 16, 2011

First parameter is a pointer to the chars. c_str() gives you that pointer from a string object. Second parameter is optional. It would contain a pointer to the next char after the numerical value in the string. See http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/ for more infos.

string s;
double d;

d = strtod(s.c_str(), NULL);