Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Don't ever write code like that.</h2> <hr> <p>For <code>j&lt;1000</code>, <code>j/1000</code> is zero (integer division). So:</p> <pre><code>(&amp;main + (&amp;exit - &amp;main)*(j/1000))(j+1); </code></pre> <p>is equivalent to:</p> <pre><code>(&amp;main + (&amp;exit - &amp;main)*0)(j+1); </code></pre> <p>Which is:</p> <pre><code>(&amp;main)(j+1); </code></pre> <p>Which calls <code>main</code> with <code>j+1</code>.</p> <p>If <code>j == 1000</code>, then the same lines comes out as:</p> <pre><code>(&amp;main + (&amp;exit - &amp;main)*1)(j+1); </code></pre> <p>Which boils down to</p> <pre><code>(&amp;exit)(j+1); </code></pre> <p>Which is <code>exit(j+1)</code> and leaves the program.</p> <hr> <p><code>(&amp;exit)(j+1)</code> and <code>exit(j+1)</code> are essentially the same thing - quoting C99 §6.3.2.1/4:</p> <blockquote> <p>A function designator is an expression that has function type. Except when it is the operand of the sizeof operator <em>or the unary &amp; operator</em>, a function designator with type "<strong>function returning type</strong>" is converted to an expression that has type "<strong>pointer to function returning type</strong>".</p> </blockquote> <p><code>exit</code> is a function designator. Even without the unary <code>&amp;</code> address-of operator, it is treated as a pointer to function. (The <code>&amp;</code> just makes it explicit.)</p> <p>And function calls are described in §6.5.2.2/1 and following:</p> <blockquote> <p>The expression that denotes the called function shall have type <strong>pointer to function</strong> returning void or returning an object type other than an array type.</p> </blockquote> <p>So <code>exit(j+1)</code> works because of the automatic conversion of the function type to a pointer-to-function type, and <code>(&amp;exit)(j+1)</code> works as well with an explicit conversion to a pointer-to-function type.</p> <p>That being said, the above code is not conforming (<code>main</code> takes either two arguments or none at all), and <code>&amp;exit - &amp;main</code> is, I believe, undefined according to §6.5.6/9:</p> <blockquote> <p>When two pointers are subtracted, <strong>both shall point to elements of the same array object</strong>, or one past the last element of the array object; ...</p> </blockquote> <p>The addition <code>(&amp;main + ...)</code> would be valid in itself, and could be used, <em>if</em> the quantity added was zero, since §6.5.6/7 says:</p> <blockquote> <p>For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.</p> </blockquote> <p>So adding zero to <code>&amp;main</code> would be ok (but not much use).</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