Just adding to the existing answers.
The access
*(a+5) = a[5]
So this is the location not allocated by you.
In the case of array say
int a[6];
You have a valid access from a[0]
to a[5]
where a[5]
is the last element of the array and any further access like a[6]
will lead to undefined behavior as that location is not allocated by you.
Similarly you just have a integer allocated like
int b=5;
int *a = &b;
a is a pointer pointing to &b
i.e address of b.
So the valid access for this is just a[0]
which is the only location allocated by you on the stack.
Any other access like a[1] a[2]...
and so on will lead to undefined behavior.
The access turns out to be VALID if you have something like
int b[6];
int *a = b;
Now a[5]
will give the value of the last element of the array b