A small puzzle. Why does this code work the way it does? This was given to me by a friend of mine, it prints out 0 through 9. We have been quite puzzled over this. Does anyone know why this works?

#include &lt;iostream&gt;

using namespace std;

void x(int a)
{
    *(&amp;a-1) += a;
}

int main()
{
    for(int i = 0; i &lt; 10; ++i)
    {
        x(8);
        i = 0;
        cout &lt;&lt; i &lt;&lt; endl;
    }
}
First of all, the line *(&amp;a-1) += a; is undefined behavior, so this could do anything depending on compiler version, flags used, alignment of the stars, etc.

That said, the reason why it works in your case is probably because &amp;a-1 happens to point to the return address of x, and incrementing it by 8 causes the i = 0; to be skipped.