-2

I tried to cast an integer into a float via pointer. When I do pointer type casting as follows:

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

    b = (float*)&a;

    printf("b = %f\n", *b);

    return 0;
}

The result is

b = 0.000000

On my platform, float and int are both 4-bit. Can anyone let me know why I cannot get b = 10.0? Thanks!

melpomene
  • 84,125
  • 8
  • 85
  • 148

3 Answers3

2
  1. When you convert pointer types and use the new pointer to access an object, your C implementation may attempt to reinterpret the data1 as the new type. However, it reinterprets the bits in memory; it does not convert the value. The encoding for the int value 10 is the 32 bits 00000000000000000000000000001010. For float, those bits are the encoding for the value 1¼ • 2−146 (assuming the most common encoding for float is used by your C implementation). This float value is so small that, when it is formatted with %f, it is rounded to zero.

  2. Do not use pointer conversions to convert or reinterpret data. To convert types while retaining the value (as much as possible given the formats), use a cast, such as float b = (float) a;. (In many cases, the assignment itself will serve to convert in this way.) To convert types while reinterpreting the bits that encode the value, use either memcpy (such as memcpy(&b, &a, sizeof b); or a union. When doing this, you must ensure that the source and destination objects are the same size, and there may be implementation-dependent effects, because not all C implementations encode values in the same ways.

  3. float and int are four bytes (in your C implementation), not four bits.

Note

1 In general, you should not convert pointers to reinterpret data. There are some exceptions to this, notably that you may use character types to access the data of an object.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Hi Eric, your answer really helps and is exactly what I'm looking for! Thank you so much! BTW, can you explain how you converted 00000000000000000000000000001010 to a decimal? I know general rules of converting binary into decimal, but since this number's exponent is 0, I totally have no idea how to do the conversion... Thanks again! – ottersailor Nov 30 '18 at 22:27
  • @ottersailor: I have answers about how to decode IEEE-754 basic 32-bit binary encodings [here](https://stackoverflow.com/a/20633632/298225) and [here](https://stackoverflow.com/a/6313398/298225). Although what I actually did is use `printf` with `%a`, which shows the value in a “hexadecimal floating-point” format from which it is easy to read the power of two. – Eric Postpischil Nov 30 '18 at 22:35
  • Thank you Eric. The links are very helpful!! – ottersailor Dec 01 '18 at 02:02
1

You're not converting int 10 to a float. Rather, you are setting the b pointer equal to the address of a. NO casting has taken place. The only thing your cast has accomplished to turn off error checking.

What do you want? If you want b to hold to hold a pointer to a float represenation of 10, then you would need to do this:

float b_float;
bptr = &b_float;
*bptr = (float) a;

Is that what you are trying to do?

Gardener
  • 2,591
  • 1
  • 13
  • 22
0

You're not converting the value 10 from int to float, but converting the representation of the int with value 10 to a float.

Integers are typically stored as a simple series of bytes, basically as a number in base 256. Floating point types on the other hand have a very different representation, typically IEEE 754.

dbush
  • 205,898
  • 23
  • 218
  • 273