We have to first think that the meaning of the *ptr, is pointing to the value that is being located at the address location.
Then we have to consider the below points.
++*ptr: means the value that is pointing to a particular location is incremented by one.
Which is equal to pre-increment operation
*ptr++:means that the value that is pointing to the particular address location is incremented by one.
Which behaves like the post-increment operator.
Let us see the example below:
Here we assume that the address of the integer value is 5000.
1 |
Address of Num : 5000 |
The result of the ++*ptr is evaluated as shown below
1 2 3 4 5 6 7 8 9 |
++*ptr = ++ *ptr = ++ *(5000) = ++ (Value at address 5000) = ++ 15 = 16 |
The working example for ++*ptr:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main () { int iVal = 15; int *ptrVal = NULL; ptrVal = &iVal; cout << "Value of ++*ptrVal :" << ++*ptrVal << endl; getchar (); return 0; } |
The output of the above program is:
1 |
Value of ++*ptrVal :16 |
Now go for another situation. i.e. *ptr++ and let us see how the result is
Here in the same way as we assume the address of the ptr is 5000.
1 |
Address of Num : 5000 |
The result of the *ptr++ is evaluated as shown below
1 2 3 4 5 6 7 8 9 |
*ptr++ = *ptr++ = *(5000)++ = (Value at address 5000)++ = 15 ++ = 15 |
The working example for ++*ptr:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main () { int iVal = 15; int *ptrVal = NULL; ptrVal = &iVal; cout << "Value of *ptrVal++ :" << *ptrVal++ << endl; getchar (); return 0; } |
The output of the above program is:
1 |
Value of *ptrVal++ :15 |
So by seeing the above examples we can easily find the differences between the ++*ptr and *ptr++ .