2

Consider the following code:

#include <stdio.h>
int main(void){
    char *p="A for Apple";
    char q[15]="B for Ball";
    printf("x=%c y=%c\n",*(p+10)-2,*(&q[4]-2)); 
    printf("u=%c v=%c w=%d\n",p[8],*q+8,8[p]-q[8]);
    return 0;
}

Output:

x=c y=f  
u=p v=J
w=4

Problem, I am having here is determining how w=4 was evaluated.

What does 8[p] means?

Omarito
  • 577
  • 6
  • 22
So Lo
  • 151
  • 6

1 Answers1

3

a[x] is shorthand for *(a + x). Consequently, a[x] is equivalent to the expression x[a]. (same holds for 8[p] and p[8] of course ;))

knittl
  • 246,190
  • 53
  • 318
  • 364