Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Dereferencing a character pointer got you the first character-sized chunk of the internal representation of 1000 as an integer. That character was then promoted to a <code>int</code> (as per the rules for varidac arguments), and interpreted as integer.</p> <p>On you machine, with that compiler, the result was -24.</p> <p>Character sized chunk was <em>probably</em> a 8-bit byte.</p> <p>The integer was <em>probably</em> represented by 4 or 8 of those bytes.</p> <p>The internal representation was <em>probably</em> 2s-complement, and was <em>probably</em> stored in little endian order.</p> <p>Do you begin to see why the result wasn't easy for you to predict?</p> <hr> <p>Doing the same thing with a float pointer returned a <code>float</code> sized chuck of memory instead of a <code>char</code>, and now you may have <em>real</em> (heh!) trouble because we don't know, at this point if the <code>float</code> representation will even fit in a <code>int</code> representation, so you may be accessing uninitialized memory.</p> <p>In any case, when you dereferenced the <code>float*</code> it interpreted that memory as <code>float</code> (which has a rather more complex internal structure than a character or even a 2s-complement integer), and promoted it to a <code>double</code> (those rules for varidac arguments again) <em>then</em> an attempt was made interpret that representation as an integer (and if the <code>float</code> <em>did</em> fit in an <code>int</code>, the <code>double</code> probably <em>doesn't</em>, so you're interpreting <em>part</em> of the double as an int!). Ugh.</p> <p>This situation is even worse than the last because it involves the IEEE floating point representation standards and two chances to have the sizes not match up at all.</p> <p>So the lesson here is:</p> <h2>Don't do that.</h2> <p>And the subsidiary lesson, only for experts is</p> <h2>Still don't do it.</h2> <p>because if you want to perform all these silly reinterpretations you want to have exact and explicit control of them.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload