Search code examples
c++pointerspost-increment

Why do pointers behave differently when operated by post increment oparator?


Let's see the first code:

The following code displays the value of n=10:

#include<iostream>
int main()
{
    int n=10;
    int*p=&n;
    *p++;
    std::cout<<n;
    return 0;
}

The following code displays the value of n=11:

#include<iostream>
int main()
{
    int n=10;
    n++;
    std::cout<<n
    return 0;
}

Solution

  • p++ increments the pointer. You would need (*p)++ to increment the value.