Difference between ++*ptr and *ptr++ in Cpp with examples and pictorial representation:
We have to first think that the meaning of the *ptr, it 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 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.
The result of the ++*ptr is evaluated as shown below
Let u see the pictorial representation of the above scenario
Here in the above diagram it is referring to the same address location and the value being incremented.
The working example for ++*ptr:
The output of the above program is
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.
The result of the *ptr++ is evaluated as shown below
The working example for ++*ptr:
The output of the above program is:
So by seeing the above examples we can easily find the differences between the ++*ptr and *ptr++ .
We have to first think that the meaning of the *ptr, it 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 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.
Address of Num : 5000
The result of the ++*ptr is evaluated as shown below
++*ptr = ++ *ptr = ++ *(5000) = ++ (Value at address 5000) = ++ 15 = 16
Let u see the pictorial representation of the above scenario
The working example for ++*ptr:
#include "stdafx.h"
#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
Value of ++*ptrVal :16See the output is exactly matched with the value that we are showing in the above diagram.
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.
Address of Num : 5000
The result of the *ptr++ is evaluated as shown below
*ptr++ = *ptr++ = *(5000)++ = (Value at address 5000)++ = 15 ++ = 15Let us see the pictorial representation of the above scenario.
The working example for ++*ptr:
#include "stdafx.h" #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:
Value of *ptrVal++ :15That is same as the value that is shown in the figure above.
So by seeing the above examples we can easily find the differences between the ++*ptr and *ptr++ .
No comments:
Post a Comment