Reputation: 585
#include <stdio.h>
int main(void)
{
char c[] = "Gate2011";
char *p = c;
printf("%s", p+p[3]-p[1]);
return 0;
}
Output: 2011
Why does it give this output? I tried different combinations and it always gives junk.
Upvotes: 0
Views: 202
Reputation: 1
p+p[3]-p[1]
for p is the address of Ram address of string "Gate2011" ; for P[3] - p[1] will make actually a offset of visit to string ,that is 'e' - 'a' = 4
you can count where is it now.
Upvotes: 0
Reputation: 35408
because p[3] = 'e' = 101
and
p[1] = 'a' = 97
101 - 97 = 4
p + 4
= address of "2001" in "Gate2001"
interpreted as string ... there you go.
I also do not understand the downvotes :(
Upvotes: 3