I am seeking for an explanation regarding how incrementing an address affects a pointer.
I learned about how C pointers work and how incrementing a pointer is done by considering the pointer type. still I don't understand the following case
int main()
{
int a[] = {1,2,3,4,5};
int *p = (int*)(&a+1);
printf("%d\n%d\n", *(a+1), *(p-1));
return 0;
}
I expected this line
int *p = (int*)(&a+1);
to make p point to the address that follows array a, therefore I expected the output:
2
as it is simply a[1]
But the output was unknown_number - as I don't know which int is 4 bytes behind (&a+1)
And the actual result is:
2
5
Why does it seems that p points directly to the memory sitting after a?
What is the source for my confusion?