#include <stdio.h>
int main(void)
{
int a[5]={1,2,3,4,5};
int *ptr=(int*)(&a+1);
printf("%d %d\n",*(a+1),*(ptr-1));
return 0;
}
Output:
2,5
I could not understand how the *(ptr-1) evaluated to 5 (correct output). But when I manually
did it was 1. My understanding is *(ptr-1) will get evaluated to *(&a+1-1) which would be
*(&a) which is 1.
Please help me to understand this concept.
makes
&a + 1makes&a + sizeof (a)asais type ofint [5]which makesptrto point to actuallya[5](invalid/beyond the defined limit)(ptr - 1)points thus points toa[4]and*(ptr - 1)will print5.