When you use arr
it can naturally decay to a pointer to its first element, i.e. it's equivalent to &arr[0]
.
When you do arr++
you attempt to modify this pointer (effectively doing arr = arr + 1
). This is not possible, since the location of the array is fixed and can not be modified.
With arr + 4
you're doing pointer arithmetic. Here you add 4
to the decayed pointer, but you do not attempt to modify the pointer itself. This is equal to &arr[4]
in fact.
There is another small problem in the code you show. I say small but I mean big.
With
printf("%u", arr);
you let the array decay to a pointer. Then you attempt to print this pointer using the "%u"
format specifier. But that format specifier is to print int
, not pointers. Mismatching format specifier and argument types leads to undefined behavior.
To pint a generic (void *
) pointer you need to use the "%p"
format, and cast the pointer to void *
:
printf("%p", (void *) arr);