What does 1[d=b] mean?
I was working through the 160 byte BrainFuck code trying to figure out what things do, and I cant seem to figure out what 1[d=b] does.
s[99],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++开发者_运维技巧%93;)for(b=c&2,b=c%7?a&&(c&17
?c&1?(*r+=b-1):(r+=b-1):syscall(4-!b,b,r,1),0):v;b&&c|a**r;v=d)main(!c,&a);d=v;}
Heres the code, its about midway through the first line
http://j.mearie.org/post/1181041789/brainfuck-interpreter-in-2-lines-of-cI'm not asking what it does in that context but what 1[] does in the first place.
Thanks =)
In C, there is no difference between x[7]
and 7[x]
. They both equate to *(x+7)
(and *(7+x)
since addition is commutative) which means the seventh element of the x
array.
In this particular case (1[d=b]
), you first assign the current value of b
to d
, then calculate 1[d]
which is the same as d[1]
.
By doing it this way (offset[base]
rather than base[offset]
), it allows you to combine it with the assignment, otherwise you'd need:
d = b; char *v = d[1];
I suppose I shouldn't need to tell you that this is actually very bad code, as evidenced by the fact that you have to think very hard about what it means. Better code would be almost instantly decipherable.
It indexes into the array/pointer d
(which was just assigned the value of b
). It is equivalent to
d=b;
char*v=d[1];
In c, arrays and pointers can be handled the same way in many respects; most notably that one can use pointer arithmetic the same way on both. d[1]
is equivalent to *(d + 1)
, which is trivially equivalent to *(1 + d)
- which in turn is equivalent to 1[d]
!
Using the index notation may be more typical for array types, but (especially in obfuscated code contests ;-) both can be used on either type. Don't use such tricks in real, production code though... as you can testify, it can make future maintainers' life difficult :-(
a[b] is *(a + b)
b[a] is *(b + a)
So
a[b] is b[a]
And
1[a] is a[1]
v=1[d=b]
is d=b; v=d[1];
精彩评论