2

A

int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);

B

float i=10.00;
void *p = &i; // no change
printf("%d\n", *(int*)p);

Why does A print 0.0, not 10.0? If we change A to B, then its output is garbage.

Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
  • 6
    `float` and `int` are represented differently at the binary level. Why would you expect this to work? – John3136 Oct 23 '13 at 05:02
  • You can have a look at this [thread](http://stackoverflow.com/questions/7644699/floating-point-representation) – Anand Oct 23 '13 at 05:08

3 Answers3

5

To be more precise about what the others say, here is a test:

#include <stdlib.h>

int main()
{
    int a = 10;
    float b = 10;
    char * p;

    p = &a;
    printf("int repr: %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);

    p = &b;
    printf("float repr: %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);

    return 0;
}

The output is

int repr: 0a 00 00 00
float repr: 00 00 20 41

This shows:

a) It is a little endian machine, as the lowest byte of the int comes first in memory b) the int has the representation with the bytes 0a 00 00 00, so the value is 0000000a, the hex representation of, well, 10. c) the float is indeed 41200000. According to IEEE 754, this means you have one sign bit, 8 bits of exponent and 23 bits of mantissa. The sign is 0 (+), the exponent is 0x82, meaning +3, and the mantissa is 010000..., which means 1.01 in binary or 1.25 in decimal.

Together, these data form the value 2*2*2*1.25 = 8*1.25 = 10.

glglgl
  • 89,107
  • 13
  • 149
  • 217
4

Because you're not really doing a cast in the first case -- you're casting pointer types.

If you want 10.0, here's how you'd do it:

int i = 10;
printf("%f\n", (float)i);

Here's what you're doing now:

int i = 10;                  // Create int i and initialize to 10.
void *p = &i;                // Get an untyped pointer and set it to the address of i
printf("%f\n", *(float*)p);  // Treat the pointer p as a float pointer and dereference it, printing it as a float.

Since int and float have different representations, there's no reason to believe this would work.

Christian Ternus
  • 8,406
  • 24
  • 39
  • Furthermore, the pointer cast is undefined behavior, so the program might as well crash and burn, rather than just printing garbage. I suppose it would depend on the sizes of int and float. If the same code would be using double instead of float, a crash is more likely. – Lundin Oct 23 '13 at 06:11
  • Please elaborate in detail.!! why pointer cast is undefined behavior?? and program is not crashing even if we type cast it to double pointer, it is still giving same answer.. – Amit Sharma Oct 23 '13 at 15:25
0

Basically what you are doing is you are casting a void * to a float */int *, i.e, you are casting pointer types and then dereferencing them. Since the dereferencing operation behaves differently depending on the type, there is no reason to beilieve this should work. You can try this code and see the difference :

int i = 10;
void *p = &i;
printf("%d\n", *(int*)(p));
jester
  • 3,491
  • 19
  • 30