This gets complicated, but ... it's to do with evaluation order.
Once I fix the syntax error - you are missing a semicolon from this line:
int* pnum = &num
Rejig your code a little, you can see the problem more clearly:
#include <iostream>
int incr10(int* num);
using namespace std;
int main(void)
{
int num;
int* pnum = #
num = 3;
cout << "incr10(pnum) = " << incr10(pnum) << endl
<< "num= " << num
<< endl;
num = 3;
cout << "incr10(pnum) = " << incr10(pnum) << endl;
cout << "num= " << num << endl;
num = 3;
incr10(pnum);
cout << "num = " << num << endl;
return 0;
}
int incr10(int* num)
{
*num += 10;
return *num;
}
Gives the results:
incr10(pnum) = 13
num= 3
incr10(pnum) = 13
num= 13
num = 13
Which shows the problem. It's even clearer in a smaller example:
#include <iostream>
using namespace std;
int A() { cout << "A" << endl; return 1; }
int B() { cout << "B" << endl; return 2; }
int main(void)
{
cout << A() << B();
return 0;
}
Which gives you:
B
A
12
Showing that the result of B is calculated before the result of A.
Evaluation order is unspecified, so this problem way only occur in some compilers: I used the
gdb
compiler, which evaluates right to left, but others may work the another way round!
Order of evaluation - cppreference.com[
^]