Dereferencing int but casting to a float prints nothing in C -


c noob here trying follow along online lectures. in professors example shows can read data stored in int float doing follow *(float*)&i. tried doing following code nothing happens. testing here http://ideone.com/exmxsw

#include <stdio.h>  int main(void) {     // code goes here     int i=37;     printf("%i", *(float*)&i);     return 0; } 

this causes undefined behaviour:

  • executing *(float *)&i violates strict aliasing rule
  • the wrong format specifier used: %i int, supplied float

when code causes undefined behaviour, may happen. lecture advising rubbish lecture unless showing example of not do. incorrect "we can read data stored in int float" method.

nb. ideone.com not great testing because suppresses whole lot of compiler error messages, may think code correct when in fact not.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -