>In the above program, the pointer casting during dereferencing works
>with variables but not while casting with of different types pointers.
>Why type casting of pointers of different types gives garbage values?
I believe you have been told before that the memory layout of integers and floats is quite different.
So telling the compiler to treat the value in a float as if it were an integer by type-casting a pointer to that float will yield garbage essentially. The same if you tell the compiler via a type-cast of a pointer to treat an integer as if it contains a float value.
Type-casting pointers does NOT convert the data layout in the variable being accessed. It simply instructs the compiler to access and use the contents of that object AS IF IT WERE defined (and therefore mapped in memory) as an object of the type to which it is being cast.
Representation of Int Variable in Memory in C++
https://www.geeksforgeeks.org/representation-of-int-variable-in-memory-in-cpp/
C Float and Double
https://www.geeksforgeeks.org/c-float-and-double/
IEEE Standard 754 Floating Point Numbers
https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/
How are floating point numbers stored in memory?
https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-stored-in-memory
Floating point number representation
- Wayne