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

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -