Thread: Question about syntax (should be easy, right?)

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question Question about syntax (should be easy, right?)

    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:
    Code:
    char c = 1["3"];
    or
    Code:
    char c = 2["22"];
    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:
    Code:
    char c = '\0';
    and
    Code:
    char c = '\0';
    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

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    The trick here is that in c/c++ p[n] == n[p] (for arrays only of course)

    Basically p[n] returns *(p + n) since arrays are pointers, but + is commutative so it doesn't matter if which way round the index and the pointer go (and the type checking allows it).

    So in the 2["22"] case you have a const char* which is the string literal and you are indexing the last element of it which is always '\0' (remember string literals get null terminated automatically)
    Last edited by sigfriedmcwild; 09-10-2009 at 06:50 AM.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Thumbs up

    Quote Originally Posted by sigfriedmcwild View Post
    The trick here is that in c/c++ p[n] == n[p] (for arrays only of course)

    Basically p[n] returns *(p + n) since arrays are pointers, but + is commutative so it doesn't matter if which way round the index and the pointer go (and the type checking allows it).

    So in the 2["22"] case you have a const char* which is the string literal and you are indexing the last element of it which is always '\0' (remember string literals get null terminated automatically)
    I suppose if I'd actually bothered to output what the two additional statements in my test code evaluated to, I would have figured that out.
    I never knew the language allowed you to do that (the reversal from the "normal" syntax). Thank you for the explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM