Why can a pointer variable in c be written as p[i] (i is an integer)?
This is the code
#include <stdio.h>
int main(void){
int a[3][3], *p, i;
p = &a[0][0];
for(i=0; i<9; i++) p[i]=i+1;
printf("%d\n", a[1][2]);
return 0;
}
In this example, I know the code will print 6. But since p is a pointer variable, how can it be written as p[i]. I guess that p[i] here is the same as *(p+i) and this will points to the ith element in the 2-dimension array. But my question is why are p[i] and *(p+i) the same if p is only a pointer variable?
PS: I know that if we define an array a[10], a can be seen as a pointer constant.
Edited: I think my confusion lies in why the pointer can be indexed here.
I just wonder why this notation is acceptible in C?
Read more here: Source link
