2011-04-29

C's Eccentric View Of Arrays

C compilers transform myArray[i] into *(myArray + i), which is equivalent to *(i + myArray) which is equivalent to i[myArray]. Experts know to put this to good use. To really disguise things, generate the index with a function:
int myfunc(int q, int p) { return p%q; } 
...
myfunc(6291, 8)[Array];
This looks like a rather bad joke, but who knows all the ways of C compiler...

1 comment:

dkorduban said...

The next snippet was a very popular idiom among my (former) ACM ICPC team:

for(int i = 0; i < n; ++i)
    printf(!i+" %d", a[i]);
printf("\n");

I recall first time I used it my teammate was all "WTF?" =)