Sunday, September 18, 2011

New Syntax to Array Indexing with C++

I was amazed to find a new syntax for indexing into arrays.
#include <iostream>

int a[] = {1, 12, 2, 8};

int b[] = {23, 11, 2, 3};

int main()
{
    int ind = 3;
    int c = ind[b][a];

    int d = 2[a][b];

    cout << "c = " << c << std::endl <<"d = " << d;

    return 0;
}
The Magic here is that the arrays are evaluated and indexed from left to right using integer variables or intrinsic integers.

c  =  8
d  =  2