So, i am a student still learning about programming. As i was learning i came across a pointer program that had pre and post increment. When i executed the program i was a bit confused.
Code
#include<stdio.h>
main()
{
int a=20;
int *p;
p =&a;
printf("%d\n %d\n %u\n",a,*p,p);
*p++;
printf("%d\n %d\n %u\n",a,*p,p);
p =&a;
++*p;
printf("%d\n %d\n %u\n",a,*p,p);
}
Output
20
20
146459356
20
146459360 /* this output */
146459360
21
21
146459356 /* and this */
So, how did the *p after post increment display a garbage value and why did p show address of a after pre increment? Or is there any fault of the compiler? I use Ubuntu 16.04. Detailed answer would be appreciated. Thank you.