C |字符串|问题5

下面的C程序片段打印了什么?

null

char c[] = "GATE2011" ;
char *p =c;
printf ( "%s" , p + p[3] - p[1]) ;


(A) 盖茨2011 (B) E2011 (C) 2011 (D) 011 答复: (C) 说明: 有关解释,请参见注释。


char c[] = "GATE2011"; 
 
 // p now has the base address string "GATE2011" 
char *p = c;  
 
// p[3] is 'E' and p[1] is 'A'. 
// p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4 
// So the expression  p + p[3] - p[1] becomes p + 4 which is 
// base address of string "2011" 
printf("%s", p + p[3] - p[1]);  // prints 2011
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享