I'm trying to understand why do i have this output.
a[0]: 5, a[1]: 5, ptr: 5
From this little program.
#include <stdio.h>
int main() {
int a[2] = {5, 10};
int *ptr = a;
*ptr = *(ptr++);
printf("a[0]: %d, a[1]: %d, ptr: %d\n", a[0], a[1], *ptr);
return 0;
}
The part that I don't understand is.
*ptr = *(ptr++);
Because according to me what should happen is that ptr should point to 10, and nothing more, because ++ postfix should increment ptr after the allocation, so according to me this should allocate the value of a[0] to a[0] (which would not affect anything) and after this ptr should be pointing to the 10 on a[1], but what really happens is that at the end a[1] is also 5, can someone explain this to me?.