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' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -