I came across this piece of code:
void incme(double *p)
{
*p += 1;
}
int i = 1;
incme((double *)&i); /* WRONG */
When I try to execute it, I get core dump. What is wrong with this code. Can we not type cast an int pointer to a double type.
Thank you.
You’re not casting an
intto adouble, you’re casting anint *to adouble *. That’s not safe ifsizeof(double)andsizeof(int)aren’t the same…Even if you match up the storage sizes, what do you expect the output to be? Floating-point types and integers tend not to have any kind of compatible representations.