Your program has undefined behavior: your code cannot write to *p
after it has been advanced, because at that point the pointer points to memory not allocated to an int
. Because of the way your variables are declared it appears that after the first increment the pointer is actually pointing to itself! Printing the address of the pointer &p
should confirm that.
To fix this issue, change your program as follows:
int data[] = {0,0,0,0,0,0,0,0,0,0};
int *p = data;
Now you can increment your pointer up to ten times without the risk of writing to memory outside of the range allocated to your program.
Note: %p
expects a void*
pointer, so you should insert an explicit cast.