Question about pointers

So there is a question that wants to assign the address of variable num1to pointer variable fptr. However, num1 is type float and fptr is type double. Is it possible to do the given task?
yes, you can shoot yourself on the foot.
Yes, it is possible to do this via type casting.
double *fptr = (double *) &num1;

Where, the value stored at the &num1 (num1 address) is converted to double.
To explain further: by writing to pointer of another type which is there in reality, you are invoking Undefined Behavior, which is a very bad thing.
In reality you probably mess up unrelated memory.
1
2
3
4
5
6
7
8
9
#include <iostream>

int main ()
{
    float a[3] = {1, 2, 3};
    double* z = (double*) &a[1];
    *z = -999.111;
    std::cout << a[0] << '\n' << a[1] << '\n' << a[2] << '\n';
}
1
2.12865e+012
-4.47569
Topic archived. No new replies allowed.