How can i cast a pointer to char to a double ?
I am using command line arguments and one of the argument is a double but in the program is it passed as a char*.
I tried using static_cast and reinterpret_cast but with no effect.

- 19,440
- 34
- 112
- 219
-
1Duplicate of [How can I convert string to double in C++?](http://stackoverflow.com/questions/392981/how-can-i-convert-string-to-double-in-c) – James McNellis Mar 09 '11 at 20:57
-
Just a note. You're not really interested in casting a char * to a double. You want to convert a c string representation of a double to a double. Casting a char * to a double would literally just be taking the address of a character and turning that into a numeric representation stored in a double. – patros Mar 09 '11 at 21:25
7 Answers
Pure C++ solution:
#include <sstream>
// ...
std::stringstream ss;
ss << your_char_pointer;
ss >> your_double;
Boost solution:
#include <boost/lexical_cast.hpp>
// ...
your_double = boost::lexical_cast<double>(your_char_pointer);

- 256,549
- 94
- 388
- 662
-
Both probably involve an unnecessary copy of the string; I'd rather use `std::atof()` (or `std::strtod()` if you need to report errors). – Mike Seymour Mar 09 '11 at 21:04
double val = atof(*charpointer)
atof stands for "all to float", (or "array to float"), and does exactly what you want. If it cannot convert the char array, it returns 0.0. See: Man atof

- 2,008
- 17
- 36
-
1I always thought it was "ASCII to float". Not that the system character set is necessarily ASCII, I just assumed it was named by someone who invented it for a system where it was. Any records one way or another? – Steve Jessop Mar 09 '11 at 20:59
-
@Steve Hmm I do indeed find more references for ASCII. Interesting, I was pretty sure that it was one of the aforementioned two. Well, thanks for correcting me in that case :) (although the most reliable source thus far is wikipedia) – Yuri Mar 09 '11 at 21:05
-
1Indeed, I'm not certain, and that Wikipedia page might have been written by someone like me, to whom it had never occurred that it might stand for anything else. – Steve Jessop Mar 09 '11 at 21:11
That's not how type conversion in C/C++ works. You must pass the string through a numeric parser manually. E.g.
char *thestring;
double d;
d = atof(thestring);

- 159,371
- 13
- 185
- 298
You're trying to convert a string (represented by the char *
) into a double
. This is not something you can do with a regular built in type cast in C++ as all they do is reinterpret the bit pattern that is being referenced by the pointer. Instead you have to parse the command line argument to extract a double value from the string.
As mentioned, you have several options:
- you can use
atof
for the conversion, but it's hard to determine if the conversion errored because both a string that can't be converted and one representing 0.0 give you the same result - As Fred Larson mentioned, you can use
boost::lexical_cast
. That's a pretty elegant way to handle the problem and would most likely be my preferred one - You can use iostreams to do the conversion
- You can write the conversion code yourself (just kidding)

- 24,095
- 5
- 52
- 70
The atof
man page says "The atof() function has been deprecated by strtod() and should not be used in new code."

- 1,138
- 1
- 8
- 16