Float pointer to int - C

Hello,

Could you please tell me why y is 1086219904.000000 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>

int main()
{
	float x, y;
	int* p;
	x = 5.95;
	p = &x;
	y = *p;
	printf("\n y = %f",y); //  y = 1086219904.000000


	return 0;
}
y is the address of x converted to a float.

You could look at that in a debugger, on a watch list.
Is my understanding correct?

First of all: x converted to binary representation:
5.95 --> 01000000101111100110011001100110
then binary to int:
01000000101111100110011001100110 ---> 1086219904
Does your code actually compile, @Shervan360?
In C yes, but for C++ no
@lastchance
c++ is more strongly typed.
you just need a cast:
p = (int*)&x;
this assumes int and float have the same size, which they may or may not. Most systems, they do.

Topic archived. No new replies allowed.