Edit Your post is very unclear. Assuming data
is actually a pointer to an array of double the solution is even simpler:
double * pd = static_cast<double const *>(data);
double d0 = pd[0];
double d1 = pd[1];
or (C):
double * pd= (double const *)(data);
double d0 = pd[0];
double d1 = pd[1];
You cannot perform pointer arithmetics and dereferencing on void *
so *data
or data + 8
are invalid.
I don’t understand exactly what you are trying to do, but here is how you access data in your array:
Assuming the data stored is actually int
, to access the 8th element and convert it into double you should write:
double el = static_cast<double>*(static_cast<int const *>(data) + 8));
better yet:
int *p = static_cast<int const *>(data);
double el = static_cast<double>(p[8]);
Please note that is is really important that you convert data
to the original pointer type . In my example I used int
but you should replace it with what your data actually is.
On that note, if what you are showing as {0,0,0,0,0,0,0,40,20,0,0,0,0,0,0}
are actually bytes, then the original pointer type is char *
and you should convert to char *
.
To clarify furthermore converting data from pointers:
Let’s say you have a pointer to int
:
int i = 24;
int *pi = &i;
How would you convert to double the data found on address pi
?
// wrong:
double *pd = &i;
cout << *pd << endl;
That is wrong because what you do is converting the pointer type and not the actual data. In effect you interpret the bit pattern found at address pi
as a bit pattern representing a double
. But that bit pattern represents and int
.
What you need to do is retrieve the data as it is: an int
and then convert the int
to double
:
int x = *pi;
double d = (double) x;