I came along a competitive question that asks the output of the following:
#include <stdio.h>
int main()
{
int a[] = {0,1,2,3,4};
int i, *ptr;
for(ptr = a+4, i=0; i <=4; i++)
printf("%d", ptr[-i]);
return 0;
}
I did read this topic: Are negative array indexes allowed in C? However it was unclear to me how the -ve symbol generates the array in the reverse order, ie. 4, 3, 2, 1, 0.
ptrand index-ito calculate pointer arithmetic to some element withina[]. The arraya[]is always rooted at0and dereferencing into it viaa[n]for somenless than zero is undefined behavior. Arrays aren't pointers. The tidal wave of answers below explain the pointer math concept well.a[b]==*(a + b)==b[a]