#include<stdio.h>
int main()
{
int x;
int *ptr;
ptr = &x;
*ptr = 0;
printf("x= %d\n", x);
printf("*ptr= %d\n", *ptr);
*ptr = 5;
printf("x= %d\n", x);
printf("*ptr= %d\n", *ptr);
(*ptr) ;
printf("x= %d\n", x);
printf("*ptr= %d\n", *ptr);
return 0;
}
Acc. to precedence brackets will be first simplified and then
and therefore it will be post increment :
(*ptr)
(third portion of question)
value should be 5 as it is post increment operator i.e it will first assign the value and then increment..In this case also it should first assign value and then increment..But answer coming is
6
CodePudding user response:
int *ptr;
(*ptr) ;
increases the value of the object referenced by the pointer. The result is the same as*ptr = 1;
*ptr ;
increases pointer itself. The result is the same asptr = 1;
value should be 5 as it is post increment operator i.e it will first assign the value and then increment.
No, the sequence point, in this case, is the semicolon. So this line simple increases the object referenced by the ptr
.
To see what you want you need to use the post-increment operator directly in the more complex expression, loop or function call.
printf("*ptr= %d\n",(*ptr) );
printf("*ptr= %d\n",x);
CodePudding user response:
(*ptr) ; // increment the value (to 6)
// other stuff
printf( .... // print the value
The (post)-increment occurs substantially before the value is fetched to be printed.