printf(&unix["\021C%set"],(unix)["Chb"]+"Trick"-0X67);
它给出的输出为“Cricket”。但我不明白为什么? http://ideone.com/fTEAHG
最佳答案
unix
是 predefined macro indicating it's a Unix-like system .
在 C 中,index[array]
等同于 array[index]
。正如 MSDN 所解释的那样:
Usually, the value represented by postfix-expression is a pointer value, such as an array identifier, and expression is an integral value (including enumerated types). However, all that is required syntactically is that one of the expressions be of pointer type and the other be of integral type. Thus the integral value could be in the postfix-expression position and the pointer value could be in the brackets in the expression or subscript position.
所以
printf(&unix["\021C%set"],(unix)["Chb"]+"Trick"-0X67);
翻译成
printf(&"(\021C%set"[1]),"Chb"[1]+"Trick"-0X67);
&("\021C%set[1])
取"\021C%set"第一个元素的地址,相当于取"C%set"的地址(由于 C 指针算法)。简化它,并重新排列一些操作数:
printf("C%set","Trick"+"Chb"[1]-0X67);
"Chb"[1]
是'h'
,也就是ASCII值0x68,所以"Chb"[1]-0X67
是 1,"Trick"+1
是 "rick"
(由于 C 指针算法)。所以代码进一步简化为
printf("C%set","rick");
打印“Cricket”。
关于c - 下面的表达式会输出什么,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28994470/