Cast char* to double - as bytes

c++, c-strings, casting, double

Solution

Why did it fail?

You have a typo here.

std::cout<<(*((*double)number))<<" is my number.\n";

It should be:

std::cout<<(*((double*)number))<<" is my number.\n";

and what should I do?

You could reduce the number of parenthesis used.

std::cout<< *(double*)number <<" is my number.\n";

You should use C++ casts instead of C casts, so it's clear what you're doing.

std::cout<< *reinterpret_cast<double*>(number) <<" is my number.\n";

Problem

I have a byte array that represents double: ``` char number[8]; ``` I need to cast this to actual double (which has 8 bytes as well). Based on advice I tried this, but it failed: ``` std::cout<<(*((*double)number))<<" is my number.\n"; ``` Why did it fail and what should I do? I can, of course, extract the data using some `<<` magic, but I don't want to do this - it would consume memory and make code too robust.

Original source