C Are ++*ptr and *ptr++ are same ?
Question : Are ++*ptr and *ptr++ are same ?
Answer :
No
- ++*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 10 because increment operation will be done in the next statement because it is post increment operation.
- 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 ++
= 10
**(Post increment will first assign and then do increment)
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 :
10
Conclusion :
In short ++*ptr and *ptr++ are not same
