[ad_1]
The &
is the address-of operator. It returns the address of the variable that stores a certain value. Normally, one would do float *f_ptr = &f_var;
to get a float pointer to a value of type float. If you instead use long *l_ptr = (long *)&f_var;
you get a pointer of type long*
pointing to the same variable (which is still a float). This is dangerous and will rarely give the expected result, for two reasons: a) sizeof(long)
might be larger than sizeof(float)
, causing an access to l_ptr
to result in undefined behavior. b) Reading the value with long l = *l_ptr
will be mostly useless, since a float is not a long, and no value conversion took place. The value of l will not be the integer part of the float, as you might expect.
This double *d_ptr = (double *)l_ptr;
has similar problems: While now, both sides are pointers, interpreting a pointer to a long as a pointer to double is only useful in very rare cases (e.g. when working with unions). And double is now for certain longer than float, so doing double d = *d_ptr
will result in undefined behavior.
Bottom line: Neither of these instructions are useful, at least certainly not in beginners code.
[ad_2]