Reputation: 113
I'm running this code and getting some garbage value while printing *ptr. What could be the possible reason and how can I avoid getting that?
# include <stdio.h>
int main()
{
int test = 1;
int *ptr = &test;
*ptr++ = 10;
test++;
printf("\nThe value is %d", *ptr);
}
Upvotes: 1
Views: 90
Reputation: 280
The statement *ptr++
increments the pointer which could not be valid for your program.
Rewrite the statement as *ptr = 10;
Upvotes: 0