0
#include <stdio.h>
int main(void) 
{
   char c[]="GATECSIT2017";
   char *p=c;
   printf("%s", c+2[p]-6[p]-1);
   return 0;
}

What do 2[p] and 6[p] mean? Please provide a detailed explanation.

Output: 17

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Abhishek
  • 49
  • 6
  • 1
    http://c-faq.com/aryptr/joke.html – David Ranieri Feb 11 '17 at 18:50
  • 1
    `2[p]` is same as `p[2]`, `6[p]` is same as `p[6]` So p[2] = 'T' and p[6] = 'I' i.e `c + 84 - 73 -1` i.e. c+10. ( Substituting ASCII values of characters `c[10]` is pointing to `1` in the string. When we print it as string beginning from 10th index, the output will be 17 – Rishi Feb 11 '17 at 18:59

2 Answers2

4

For any valid pointer or array p and index i, the expression p[i] is equal to *(p + i). And due to the commutative property of addition *(p + i) is equal to *(i + p) which is then equal to i[p].

In short, 2[p] is the same as p[2].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

What do 2[p] and 6[p] mean?

2[p] is equivalent to p[2] and 6[p] is equivalent to p[6].

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
msc
  • 33,420
  • 29
  • 119
  • 214