I found this program in a contest question paper:
#include <iostream>
void main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int*)(&a + 1);
printf("%d %d ",*(a + 1), *(ptr - 1));
}
The output is 2 5
now when I change the 5th line to int *ptr=(int*)(&a); and printf("%d %d ",*(a + 1), *(ptr));
The output becomes 2 1
In the first case the ptr got the last address of the array+1 and in the second case the ptr got the same address of the array(address of a).
My doubt is why does this assignment show different kind of behavior when the a is incremented and assigned to ptr and when a is assigned to ptr without incrementing?