Are ++*ptr and (*ptr)++ are same ?
Question : Are ++*ptr and (*ptr)++ are same ?
Answer :
Yes
- ++*ptr Increments the Value being Pointed to by ptr
- Suppose ptr is Pointing to the Integer Variable having value 10. [ num = 10 ]
- Now ++*ptr Results in 11
- (*ptr)++ means Grab the Value of (*ptr) And then Increment It , which yields again 11
- This Proves that both are equivalent
Step by Step Explanation of : ++*ptr
Address of Num : 1000 ++*ptr = ++ *ptr = ++ *(1000) = ++ (Value at address 1000) = ++ 10 = 11
Step by Step Explanation of : (*ptr)++
Address of Num : 1000 (*ptr)++ = (*ptr)++ = *(1000) ++ = (Value at address 1000) ++ = 10 ++ = 11
Example 1 : Consider ++*ptr
#include<stdio.h> int main() { int num = 10; int *ptr; ptr = # printf("Value of ++*ptr : %d",++*ptr); return(0); }
Output :
11
Example 2 : Consider (*ptr)++
#include<stdio.h> int main() { int num = 10; int *ptr; ptr = # printf("Value of (*ptr)++ : %d",(*ptr)++); return(0); }
Output :
11
Conclusion :
In short ++*ptr and (*ptr)++ are same