-1
#include <stdio.h> 
int main(int argc, char *argv[]){
int *ia[5]={0,1,2,3,4}; 

iap=ia; 
printf("ia[3]:%d\n",3[ia]); 
return 0; 

}

Why is that line working in C?

Alejandro Camba
  • 978
  • 10
  • 25

1 Answers1

1

The amazing world of C pointer arithmetic:

ia[3] is computed as *(ia + 3), which is the same as *(3 + ia) or 3[ia].

You can also write 1[ia+2], or 3[ia-+!ia], or even 2[1+ia, ia+1]...

None of these should appear in regular code unless you are trying to obfuscate and confuse the casual reader/maintainer/code reviewer.

chqrlie
  • 131,814
  • 10
  • 121
  • 189