I'm just learning, and I've been playing with a copy of the GNU C Tutorial for fun. One of the questions asks "Write a statement which converts a pointer to an integer into a pointer to a double type." I think what the question is getting at is something like "my_double_ptr = (double *) my_int_ptr;".
But printing the values with printf didn't give me what I expected. So I tried to do something simpler first, and this is what I wrote just to see what would happen:
Code:
int main ()
{
float my_float = 12.3456;
float *my_float_ptr;
int *my_int_ptr;
my_float_ptr = &my_float;
my_int_ptr = (int *) my_float_ptr;
printf("My_float_ptr is %p\n", my_float_ptr);
printf("My_int_ptr is %p\n", my_int_ptr);
/* printf("My_float_ptr points to float %f\n", *my_float_ptr);
printf("My_float_ptr points to int %d\n", *my_float_ptr); */
printf("My_int_ptr points to float %f\n", *my_int_ptr);
printf("My_int_ptr points to int %d\n", *my_int_ptr);
return 0;
}
When I run it with commented lines included, I get the output:
My_float_ptr is 0x22efc4
My_int_ptr is 0x22efc4
My_float_ptr points to float 12.345600
My_float_ptr points to int -2147483648
My_int_ptr points to float 12.345598
My_int_ptr points to int 1095075732
But when I run it as shown, with comments commented, what I get is:
My_float_ptr is 0x22efc4
My_int_ptr is 0x22efc4
My_int_ptr points to float 0.000000
My_int_ptr points to int 1095075732
I can understand if ints and floats are represented differently in storage, then 12.3456 may print out as some screwy int. But what confuses me is why it outputs "My_int_ptr points to float 0.000000" in one case and "My_int_ptr points to float 12.345598" in the other, when all I did was remove a few printf lines.
Does anyone have any insight into this, or can anyone direct me to some documentation on the web where this might be explained?
Also, gcc -dumpmachine gives me "i686-pc-cygwin" and gcc -dumpversion gives me "3.3.3".