0

I am new to C++ and am having an issue converting a signed char to a double. Initial idea was to convert the signed char to a const char* and use atof to return the double.

signed char x = '100';
const char * cChar = x;
std::cout << atof(cChar);

What can I try to resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
XanderLynn
  • 883
  • 3
  • 16
  • 29
  • 6
    That first line isn't valid C or C++. Do you mean `signed char x = 100;` or `const signed char *x = "100";`? – Oliver Charlesworth Apr 07 '11 at 15:01
  • 2
    I think '100' is valid actually - a multi-character constant. It's value is some compiler-dependent integer function of '1', '0' and '0'. – paperjam Apr 07 '11 at 15:04
  • 2
    @Oli: In C++ the first line is *valid*, but the value of '100' is implementation-defined (it's a multi-character literal, 2.13.2/1), and unless that value is in range the result of converting it to `signed char` is also implementation-defined (signed overflow, 4.7/3). In C, I can't be bothered to look up the references in another standard ;-) – Steve Jessop Apr 07 '11 at 15:06
  • A character, a pointer to a character, and a string, are all different things in C++. It's not clear to me which ones you want to convert between. `const char *cChar = x` is nonsense if `x` has type `signed char`. – Steve Jessop Apr 07 '11 at 15:10

5 Answers5

2
signed char x = 100;
double d = x;
cout << d;

const char * x = "100";
double d = atof(x);
cout << d;

'100' is wrong - you need either a const char * x = "100"; or a char x=100;

Erik
  • 88,732
  • 13
  • 198
  • 189
1

Or do you mean

const char *x = "100";
std::cout << atof(x);

?

paperjam
  • 8,321
  • 12
  • 53
  • 79
  • Actually, I think this is probably the right answer, rather than my own. I hadn't spotted the single quotes, implying that the originating poster is trying to work with a string. Obviously the confusion is that in C, char is usable and often used just to store a small number. – Tommy Apr 07 '11 at 15:03
1

You might want to use strtod() or you can use boost::lexical_cast<>

yasouser
  • 5,113
  • 2
  • 27
  • 41
0

It should just be:

double value = static_cast<double>(x);

What you're currently doing is creating a pointer to memory address 100, which you almost certainly don't own, then attempting to read a string from there, which almost certainly isn't there.

(note: I originally suggested the C-style version, double value = (double)x;, see the comments below as to why C-style casts are better avoided in C++)

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • No, that is completely wrong here. No pointer is created here and such a cast would be questionable. – Konrad Rudolph Apr 07 '11 at 15:09
  • It would however, be the correct way to convert "signed char to double" as per the question. At the time of posting, it's still not clear whether the originating poster intends to be dealing with an array of char (ie, a string) or a single signed char. – Tommy Apr 07 '11 at 15:26
  • @Tommy Recommending a C style cast in C++ is *still* the wrong answer, sorry. – Konrad Rudolph Apr 07 '11 at 15:28
  • @Konrad: assuming you've conceded the "no pointer is created here" issue; for what reason do you argue that a C-style cast is wrong? I note, for example, that it's the first suggestion offered on cplusplus.com — see http://www.cplusplus.com/doc/tutorial/typecasting/ – Tommy Apr 07 '11 at 15:39
  • @Tommy cplusplus.com gives bad advice, then. For a discussion see http://stackoverflow.com/q/527999/1968 – the rules have become pretty well established in the last years. – Konrad Rudolph Apr 07 '11 at 15:49
  • @Konrad: would you agree that arguments in that discussion can be summarised as (i) casts are exceptional, so should stand out syntactically; (ii) C casts don't indicate enough of the programmer's intent; and (iii) hence, compilers are unable to check that you're doing something reasonable and warn you if you're not? I take all those points in retrospect, though I'd go with a C-style cast being strongly undesirable rather than wrong. Thanks for the link! – Tommy Apr 07 '11 at 15:54
  • @Tommy Essentially yes. But the accepted answer (not mine) of the question actually summarises this very well: “never use C-style casts” is a much simpler rule to memorise than “never use C-style casts *except* …”. – Konrad Rudolph Apr 07 '11 at 15:57
0

I'm trying to get my head around what your current understanding is

signed char x = '100';

This line of code is taking the character '100' which doesn't exist, typically characters are one symbol long such as 'a' and '9'. Unless they are special non printable sequences such as null '\0' or newline '\n', or even escaped characters such as single quote'\''

C style string are represented by double quotations and have one hidden byte at the end that hold a null terminator, the double quote notation deals with the null for you.

if your looking to convert the string "100" to a double then the following will work

double a;
char b [4] = "100";
a = atof(b);
Gareth A. Lloyd
  • 1,774
  • 1
  • 16
  • 26