I've been scratching my head trying to figure this one out for a while and it's nearly impossible to google (though how I have tried). I'm trying to understand how the code:
or
is interpreted by a compiler. The result is that 1["3"] and 2["22"] somehow evaluate to the null character '\0', so the above code is equivalent to:
and
I first saw this syntax used in the 2004 International Obfuscated C Code Contest. {code} {hint}
...and I haven't seen it since. In the hint file, he explains that his program is obfuscated by (among other things) "standard tricks like writing '\0' as 1["3"] or 2["22"]".
Testing this in my c++ compiler shows that the expression is evaluated exactly as he stated, with no compile errors or warnings. Also, changing some of the digits in the expression caused it to evaluate to something else.
The following program:
Code:
#include <iostream>
using namespace std;
int main()
{
if (1["3"] == '\0')
cout << "1[\"3\"] == '\\0'\n";
if (2["22"] == '\0')
cout << "2[\"22\"] == '\\0'\n";
if (1["22"] == '\0')
cout << "1[\"22\"] == '\\0'\n";
if (2["3"] == '\0')
cout << "2[\"3\"] == '\\0'\n";
system("pause");
return 0;
}
gives the following output:
Code:
1["3"] == '\0'
2["22"] == '\0'
Press any key to continue . . .
I don't have any particular desire to obfuscate my code, but I would like to understand the syntax used here. What does the number before the brackets mean to the compiler? What does a string in brackets following it mean to the compiler?
In short, can anyone explain to me how 2["22"] evaluates to '\0' (and 1["22"] does not)?
Head-scratchingly yours,
Dan