What does 1[d=b] mean?

我的未来我决定 提交于 2019-12-18 18:36:38

问题


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-c

I'm not asking what it does in that context but what 1[] does in the first place.

Thanks =)


回答1:


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.




回答2:


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 :-(




回答3:


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];



来源:https://stackoverflow.com/questions/7233161/what-does-1d-b-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!