诡异的C程序

 

#include <stdio.h>
int main(){
 unsigned char result = 1["night"]*2["girls"]+4["allnight"];
 char *of_you = "Though you was strong enough and seem never died";
 printf("You got %s", result+of_you);
 return 0;
} 

 

结果是:You got died

 

解释:1["night"]就是*("night"+1),也就是'i',ascii码为105
类似,2["girls"]是'l'(114)
4["allnight"]是'i'(105)
result=(105*114+105)%256=43
result+of_you=43+"Though you was strong enough and seem never died"
指向第43个字符

 

下面的程序能得到相同的结果,数组元素的下标可以放在[]前面,而数组名放在[]中间

#include <stdio.h>
int main(){
    unsigned char result = "night"[1]*"girls"[2]+"allnight"[4]; 
    char *of_you = "Though you was strong enough and seem never died";
    printf("You got %s", result+of_you);
    return 0;
}  

 

 

posted @ 2012-05-04 17:47  长虹落日  阅读(163)  评论(0编辑  收藏  举报