#include <iostream>
using namespace std;
int main() {
int num=10;
int *ptr=NULL;
ptr=#
num=(*ptr)++; //it should increase to 11
num=(*ptr)++; //it should increase to 12 but im getting 10
//if i dont initialize num and just use (*ptr)++ it gives me 11
cout<<num<<endl;
return 0;
}
I want to know why is this happening and why am I getting 10 as output.
(*ptr)++
increases num
to 11 but returns its previous value (10), because the ++
is postfix.
So with num = (*ptr)++
, you are temporarily increasing num
to 11, but then (re)assigning it with 10.