来自GATE论文的c程序

cod*_*rix 4 c pointers

嘿所有昨天我都给了我的GATE考试,在这次考试中有一个问题:

以下C程序片段打印什么?

char c [] = "GATE2011";
char *p =c;
printf ( "%s", p+p [3] ? p [1]) ;
Run Code Online (Sandbox Code Playgroud)

选项:

(A) GATE2011
(B) E2011
(C) 2011
(D) 011
Run Code Online (Sandbox Code Playgroud)

答案是' C'.当我在我的电脑上尝试代码但是如何?

提前致谢

sha*_*oth 5

这是指针算术:

char c [] = "GATE2011";
// indices:  01234567
char *p =c;
Run Code Online (Sandbox Code Playgroud)

p[1]A字符代码,p[3]E字符代码,它们的差异(E代码减去A代码)是4,然后将该差异添加到p指针,并且您已经2011传入子字符串的地址printf().