title: In C, if I cast & dereference a pointer, does it matter which one I do first?
body: <p>In C, you can cast both simple data types like <code>int</code>, <code>float</code>, and pointers to these.</p>
  
  <p>Now I would have assumed that if you want to convert from a pointer to one type to the value of another type (e.g. from <code>*float</code> to <code>int</code>), the order of casting and dereferencing does not matter. I.e. that for a variable <code>float* pf</code>, you have <code>(int) *pf == *((int*) pf)</code>. Sort of like commutativity in mathematics...</p>
  
  <p>However this does not seem to be the case. I wrote a test program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(int argc, char *argv[]){
    float f = 3.3;
    float* pf = &amp;f;
    int i1 = (int) (*pf);
    int i2 = *((int*) pf);
    printf("1: %d, 2: %d\n", i1, i2);
    return 0;
  }
  </code></pre>
  
  <p>and on my system the output is </p>
  
  <pre><code>1: 3, 2: 1079194419
  </code></pre>
  
  <p>So casting the pointer seems to work differently from casting the value.</p>
  
  <p>Why is that? Why does the second version not do what I think it should?</p>
  
  <p>And is this platform-dependent, or am I somehow invoking undefined behaviour?</p>