Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With gcc 4.3.4, using switches -O2 -Wstrict-aliasing, dreamlax's answer will produce:</p> <pre><code>warning: dereferencing type-punned pointer will break strict-aliasing rules </code></pre> <p><strong>Added:</strong> I think the objections to caf's answer about <strong><em>endianness</em></strong> and size are reasonable, which his solution does not address (no pun intended). Dustin's suggestion for using a union cast is probably legal (although from what I read there seems to be some debate, but <em>your</em> compiler does is important than the law). But his code could be simplified (or obfuscated, depending on your taste) by the one-liner:</p> <pre><code>printf("%p\n", ((union {int (*from)(void); void *to;})funcptr).to); </code></pre> <p>This removes the gcc strict-aliasing warning (but is it 'correct'?).</p> <p>Aggregate casts won't 'work' if you are using the -pedantic switch, or are using e.g. SGI IRIX, so you'll need to use:</p> <pre><code>printf("%p\n", ((union {int (*from)(void); void *to;} *)&amp;funcptr)-&gt;to); </code></pre> <p>But regarding the original question: its origin lies in the use of -pedantic, which I think is slightly pedantic :).</p> <p><strong>Further edit:</strong> Note you cannot use <strong>main</strong> in the last example, as in:</p> <pre><code>printf("%p\n", ((union {int (*from)(void); void *to;}) main).to); // ok printf("%p\n", ((union {int (*from)(void); void *to;} *)&amp;main)-&gt;to); // wrong! </code></pre> <p>because of course <strong>&amp;main</strong> decays to <strong>main</strong>.</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