For the curious the OP's program prints the following:
punya ludevil sama keren ama punya marcadian :D |
But why?
Firstly, what was printed is not the "doubles as a string", but rather their
raw bytes cast to a C-String. Notice how there are 48 characters in the printed string (47 visible characters + 1 for the null character). Also notice that the 6 doubles take up 48 bytes of memory (assuming 8 bytes per double).
Now, what the original author of that code did was something like the following:
1. Take the first eight characters, e.g. "punya lu"
2. Place those eight characters into the memory location of a double and store the double
3. Repeat for all the characters. If the phrase is not a multiple of 8 characters, pad the end with null characters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
int main()
{
const int NUM_DOUBLES_IN_PHRASE = 6;
const char* phrase = "punya ludevil sama keren ama punya marcadian :D";
double phraseAsDoubles[NUM_DOUBLES_IN_PHRASE];
for(int i = 0; i < NUM_DOUBLES_IN_PHRASE; i++)
{
const double* addressAsDouble = (const double*) (phrase + i*sizeof(double));
phraseAsDoubles[i] = *addressAsDouble;
std::cout << phraseAsDoubles[i] << std::endl;
}
return 0;
}
|
The above prints the following. These are the same numbers in the original array:
4.2232e+257
2.68904e+161
6.20198e+223
1.23988e+224
1.36702e+161
2.25033e-307 |