0

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.

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 1
    Duplicate 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 Answers7

3

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);
fredoverflow
  • 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
3

Try Boost lexical_cast.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
3

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

Yuri
  • 2,008
  • 17
  • 36
  • 1
    I 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
  • 1
    Indeed, 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
1

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);
datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

If the double comes from the command line, it is actually a real string, you have to convert it to a double, you can't just cast it.

For example, you can use strtod for this task :

double d = strtod (mystr,NULL);
krtek
  • 26,334
  • 5
  • 56
  • 84
1

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)
Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

The atof man page says "The atof() function has been deprecated by strtod() and should not be used in new code."

ceo
  • 1,138
  • 1
  • 8
  • 16