Are ++*ptr and (*ptr)++ are same ?

Question : Are ++*ptr and (*ptr)++ are same ?


Answer :

Yes
  1. ++*ptr Increments the Value being Pointed to by ptr
  2. Suppose ptr is Pointing to the Integer Variable having value 10. [ num = 10 ]
  3. Now ++*ptr Results in 11
  4. (*ptr)++ means Grab the Value of (*ptr) And then Increment It , which yields again 11
  5. 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 = &num;
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 = &num;
printf("Value of (*ptr)++ : %d",(*ptr)++);
return(0);
}

Output :

11

Conclusion :

In short ++*ptr and (*ptr)++ are same