Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I did a simple test on passing by a pointer to a function in VC++ using Visual Studio 2017. I thought it was very simple, but it turns out not as expected...

C++
#include <iostream>

int incr10(int* num);

using namespace std;

int main(void)
{
	int num = 3;
	int* pnum = &num

	cout << endl
		<< "addressed passed =:" << pnum;

	cout << endl
		<< "incr10(pnum) = " << incr10(pnum)
		<< endl
		<< "num= " << num;  //this is not expected value 13

	cout << endl
		<< "*pnum is :" << *pnum;

	cout << endl;
	return 0;
}

//function to increment a variable by 10
int incr10(int* num)
{
	cout << endl
		<< "addressed received=:" << num;

	*num += 10;

	return *num;
}

I expect num = 13 because this value is updated inside the function incr10, but it is still num=3.

maybe I missed something? or C++ standard is updated on this term?

What I have tried:

I tried and it defied my understanding on this passing by pointer argument.
I got this result from screen printout:
addressed passed =:00D1FB74
addressed received=:00D1FB74
incr10(pnum) = 13
num= 3
*pnum is :13
Posted
Updated 3-Oct-20 2:40am
v4
Comments
Joe Woodbury 2-Oct-20 19:09pm    
It appears correct. I ran it in VC++ 17 and it worked as expected. (I vaguely wonder if there was an issue with the order of execution of cout. Try splitting the output of num to its own line and see.)
Shao Voon Wong 2-Oct-20 22:02pm    
This is because num is displayed first before incr10() is called. Try setting the C++ Standard to C++17. Under C++17, the order of execution is guaranteed.
KarstenK 3-Oct-20 4:34am    
write it as answer ;-)

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;
}

//function to increment a variable by 10
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[^]
 
Share this answer
 
Comments
CPallini 3-Oct-20 8:40am    
5.
Southmountain 3-Oct-20 13:18pm    
thank you OG! the missing semicolon is caused by coping code into this text box, some symbols got lost and I manually typed &num and forgot this ";".
Have also a look at this page: Sequence point - Wikipedia[^].
 
Share this answer
 
Comments
Southmountain 3-Oct-20 17:46pm    
thank you for sharing this link!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900