C Are ++*ptr and *ptr++ are same ?



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


Answer :

No
  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 10 because increment operation will be done in the next statement because it is post increment operation.
  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 ++
         = 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 = &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 :

10

Conclusion :

In short ++*ptr and *ptr++ are not same